View Full Version : A clock but displayed as words
ukstormer
06-07-2012, 01:15 PM
Seems to be a bit of a fashionable thing as I have seen a few hardware clocks using words instead of numbers, any idea how this would be achieved in java ?
format I am looking for is
The Time is now :
Ten minutes past
One
And after 30 minutes
The Time is now :
Ten minutes to
Two
I guess you get the idea
ApacheTech
06-07-2012, 01:46 PM
There are a number of word clocks on this site:
http://words.onlineclock.net/clocks/
One of those may suit your purposes. I'm not sure if you can download the code or use it as an API, but it gives a good starting block either way.
djr33
06-07-2012, 03:07 PM
You could program this yourself. You'd basically need a very simple translation program from numbers to text. It wouldn't be too much more than a dictionary.
And you could of course base that on the simplest clock script you can find (with numbers) as a starting point.
ApacheTech, those clocks are gimmicky-- reversed text, etc. I don't think they will be what the OP is searching for.
But there probably are some out there if you search for it.
ajfmrf
06-07-2012, 08:09 PM
This is one of the scripts I have found at javascriptsource.com
http://www.web-user.info/scripts/10/index.html
ukstormer
06-12-2012, 07:57 AM
This is one of the scripts I have found at javascriptsource.com
http://www.web-user.info/scripts/10/index.html
Many thanks for the help, the nearest I have found was the above code, only problem was it was in my opinion half finished, no refresh and only saying the approximate time. I have had a play around and expanded the code to refresh every 5 seconds and show the time in a readable format, I must profess to not being a java coder, I am just OK at playing around with code and adjusting to to suit my needs, the code below is nearly there for what I want but I am struggling with styling it correctly, what I want is to be able to set the actual minutes and hours to a larger font, eg
The time is
fourteen minutes
past ten
in the evening
Any help would be much appreciated, my code so far
<html>
<head>
<title>Word Clock</title>
<script language="Javascript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod); //auto refresh every 5 second
}
function number(x) {
if (x==1) return "one"; if (x==2) return "two"; if (x==3) return "three";
if (x==4) return "four"; if (x==5) return "five"; if (x==6) return "six";
if (x==7) return "seven"; if (x==8) return "eight"; if (x==9) return "nine";
if (x==10) return "ten"; if (x==11) return "eleven"; if (x==12) return "twelve";
return x; //default
}
function ttime(h,m) {
h = number(h)
if (m<=1 || m>59) return h+" o'clock";
if (m<=1) return "one minute " + "<br>" +y +h;
if (m<=2) return "two minutes " + "<br>" +y +h;
if (m<=3) return "three minutes " + "<br>" +y +h;
if (m<=4) return "four minutes " + "<br>" +y +h;
if (m<=5) return "five " + "<br>" +y +h;
if (m<=6) return "six minutes " + "<br>" +y +h;
if (m<=7) return "seven minutes " + "<br>" +y +h;
if (m<=8) return "eight minutes " + "<br>" +y +h;
if (m<=9) return "nine minutes " + "<br>" +y +h;
if (m<=10) return "ten " + "<br>" +y +h;
if (m<=11) return "eleven minutes " + "<br>" +y +h;
if (m<=12) return "twelve minutes " + "<br>" +y +h;
if (m<=13) return "thirteen minutes " + "<br>" +y +h;
if (m<=14) return "fourteen minutes " + "<br>" +y +h;
if (m<=15) return "quarter " + "<br>" +y +h;
if (m<=16) return "sixteen minutes " + "<br>" +y +h;
if (m<=17) return "seventeen minutes " + "<br>" +y +h;
if (m<=18) return "eighteen minutes " + "<br>" +y +h;
if (m<=19) return "nineteen minutes " + "<br>" +y +h;
if (m<=20) return "twenty " + "<br>" +y +h;
if (m<=21) return "twenty one minutes " + "<br>" +y +h;
if (m<=22) return "twenty two minutes " + "<br>" +y +h;
if (m<=23) return "twenty three minutes " + "<br>" +y +h;
if (m<=24) return "twenty four minutes " + "<br>" +y +h;
if (m<=25) return "twenty five " + "<br>" +y +h;
if (m<=26) return "twenty six minutes " + "<br>" +y +h;
if (m<=27) return "twenty seven minutes" + "<br>" +y +h;
if (m<=28) return "twenty eight minutes " + "<br>" +y +h;
if (m<=29) return "twenty nine minutes " + "<br>" +y +h;
if (m<=30) return "half "+ "<br>" +y +h;
if (m<=31) return "twenty nine minutes "+ "<br>" +y +h;
if (m<=32) return "twenty eight minutes "+ "<br>" +y +h;
if (m<=33) return "twenty seven minutes "+ "<br>" +y +h;
if (m<=34) return "twenty six minutes "+ "<br>" +y +h;
if (m<=35) return "twenty five minutes "+ "<br>" +y +h;
if (m<=36) return "twenty four minutes "+ "<br>" +y +h;
if (m<=37) return "twenty three minutes "+ "<br>" +y +h;
if (m<=38) return "twenty two minutes "+ "<br>" +y +h;
if (m<=39) return "twenty one minutes "+ "<br>" +y +h;
if (m<=40) return "twenty " + "<br>" +y +h;
if (m<=41) return "nineteen minutes " + "<br>" +y +h;
if (m<=42) return "eighteen minutes " + "<br>" +y +h;
if (m<=43) return "seventeen minutes " + "<br>" +y +h;
if (m<=44) return "sixteen minutes " + "<br>" +y +h;
if (m<=45) return "quarter " + "<br>" +y +h;
if (m<=46) return "fourteen minutes " + "<br>" +y +h;
if (m<=47) return "thirteen minutes " + "<br>" +y +h;
if (m<=48) return "twelve minutes " + "<br>" +y +h;
if (m<=49) return "eleven minutes " + "<br>" +y +h;
if (m<=50) return "ten minutes " + "<br>" +y +h;
if (m<=51) return "nine minutes " + "<br>" +y +h;
if (m<=52) return "eight minutes " + "<br>" +y +h;
if (m<=53) return "seven minutes " + "<br>" +y +h;
if (m<=54) return "six minutes " + "<br>" +y +h;
if (m<=55) return "five " + "<br>" +y +h;
if (m<=56) return "four minutes " + "<br>" +y +h;
if (m<=57) return "three minutes " + "<br>" +y +h;
if (m<=58) return "two minutes " + "<br>" +y +h;
if (m<=59) return "one minute " + "<br>" +y +h;
return "h:m"; // never reached?
}
function daytime(h) {
if (!h || h>21) return "<br>" + " at night"
if (h<12) return "<br>" + " in the morning";
if (h<=17) return "<br>" + "in the afternoon";
return "<br>" + " in the evening"; // default
}
function past_to(m) {
if (!m || m>30) return "to "
return "past "; // default
}
function words(h,m) {
if (!h && !m) { // if no time supplied, use the system time
time = new Date()
h = time.getHours()
m = time.getMinutes()
}
y = past_to(m);
z = daytime(h);
h = h % 12 // fix to 12 hour clock
if (h>12) h = 1 // the clock turns round..
if (h==0) h = 12
return "<br>" +ttime(h,m)+z
}
</script>
</head>
<body onload="JavaScript:timedRefresh(5000);">
<center>
<span style="font-family:sans serif; font-weight:bold; color:#ff0000; font-size:20px">
The time is
<span style="font-family:sans serif; font-weight:bold; font-size:30px">
<script language="JavaScript">document.write(words().fontcolor("#000000"))
</script>
</center>
</body>
coothead
06-12-2012, 02:28 PM
Hi there ukstormer,
and a warm welcome to these forums. ;)
Try it like this...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A clock but displayed in words</title>
<style>
body {
background-color:#f0f0f0;
font-family:verdana,arial,helvetica,sans-serif;
font-size:100%;
}
#time {
width:600px;
line-height:38px;
border:1px solid #999;
margin:auto;
color:#333;
text-align:center;
background-color:#fff;
box-shadow:6px 6px 6px #999;
}
#time span {
font-weight:bold;
color:#000;
}
</style>
<script>
minutes= [
'','<span>one<\/span>','<span>two<\/span>','<span>three<\/span>','<span>four<\/span>','<span>five<\/span>',
'<span>six<\/span>','<span>seven<\/span>','<span>eight<\/span>','<span>nine<\/span>','<span>ten<\/span>',
'<span>eleven<\/span>','<span>twelve<\/span>','<span>thirteen<\/span>','<span>fourteen<\/span>',
'<span>fifteen<\/span>','<span>sixteen<\span>','<span>seventeen<\/span>','<span>eighteen<\/span>',
'<span>nineteen<\/span>','<span>twenty<\/span>','<span>twenty one<\/span>','<span>twenty two<\/span>',
'<span>twenty three<\/span>','<span>twenty four<\/span>','<span>twenty five<\/span>','<span>twenty six<\/span>',
'<span>twenty seven<\/span>','<span>twenty eight<\/span>','<span>twenty nine<\/span>','','<span>twenty nine<\/span>',
'<span>twenty eight<\/span>','<span>twenty seven<\/span>','<span>twenty six<\/span>','<span>twenty five<\/span>',
'<span>twenty four<\/span>','<span>twenty three<\/span>','<span>twenty two<\/span>','<span>twenty one<\/span>',
'<span>twenty<\/span>','<span>nineteen<\/span>','<span>eighteen<\/span>','<span>seventeen<\/span>',
'<span>sixteen<\/span>','<span>fifteen<\/span>','<span>fourteen<\/span>','<span>thirteen<\/span>',
'<span>twelve<\/span>','<span>eleven<\/span>','<span>ten<\/span>','<span>nine<\/span>','<span>eight<\/span>',
'<span>seven<\/span>','<span>six<\/span>','<span>five<\/span>','<span>four<\/span>','<span>three<\/span>',
'<span>two<\/span>','<span>one<\/span>'
];
hours=[
'<span>twelve<\/span>','<span>one<\/span>','<span>two<\/span>','<span>three<\/span>','<span>four<\/span>',
'<span>five<\/span>','<span>six<\/span>','<span>seven<\/span>','<span>eight<\/span>','<span>nine<\/span>',
'<span>ten<\/span>','<span>eleven<\/span>',''
];
nmae=[
' at night',' in the morning',' in the afternoon',' in the evening'
];
function init(){
obj=document.getElementById('time');
n=new Date();
h=n.getHours();
m=n.getMinutes();
if(m==59) {
when=' minute to ';
}
if(m<59) {
when=' minutes to ';
}
if(m==30) {
when=' half past ';
}
if(m<30) {
when=' minutes past ';
}
if(m==1){
when=' minute past ';
}
if(m==0){
when='';
}
if(m>30){
h=h+1;
}
if(h<=24) {
period=nmae[3];
a=12;
}
if(h<18) {
period=nmae[2];
a=12;
}
if(h<=12) {
if(m>30){
period=nmae[1];
a=0;
}
else{
period=nmae[2];
a=0;
}
}
if(h<6) {
period=nmae[0];
a=0;
}
if((h==0)&&(m==0)){
obj.innerHTML='The time is <span>12<\/span> midnight.';
}
else {
if((h==12)&&(m==0)){
obj.innerHTML='The time is <span>12<\/span> noon.';
}
else {
obj.innerHTML='The time is '+minutes[m]+when+hours[h-a]+' o\'clock'+period+'.';
}
}
setTimeout(function(){init()},1000);
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<div id="time"></div>
</body>
</html>
coothead
ukstormer
06-12-2012, 04:42 PM
Many thanks for the help Coothead.
The code was very close to what I am after and I have made a few minor amendments.
I decided against the top line of 'The time is'
I have added line breaks to each section.
I have added code for 'to' 'from' on the hours line and removed the words from the main body.
I have removed the generic o'clock and coded for just on the hour.
Added styling to adjust the size of the hours and minutes words.
Here is the new code, maybe still have the odd bug in it but looking good to me.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A clock but displayed in words</title>
<style>
body {
background-color:#f0f0f0;
font-family:verdana,arial,helvetica,sans-serif;
font-size:100%;
}
#time {
width:380px;
line-height:30px;
border:1px solid #999;
margin:auto;
color:#333;
text-align:right;
background-color:#fff;
box-shadow:4px 4px 4px #666;
}
#time span {
font-weight:bold;
font-size:200%;
color:#000;
}
</style>
<script type="text/javascript">
minutes= [
'','<span>one<\/span> minute','<span>two<\/span> minutes','<span>three<\/span> minutes','<span>four<\/span> minutes',
'<span>five<\/span> minutes','<span>six<\/span> minutes','<span>seven<\/span> minutes','<span>eight<\/span> minutes',
'<span>nine<\/span> minutes','<span>ten<\/span> minutes','<span>eleven<\/span> minutes','<span>twelve<\/span> minutes',
'<span>thirteen<\/span> minutes','<span>fourteen<\/span> minutes','<span>fifteen<\/span> minutes',
'<span>sixteen <\/span> minutes','<span>seventeen <\/span> minutes','<span>eighteen <\/span> minutes',
'<span>nineteen<\/span> minutes','<span>twenty<\/span> minutes','<span>twenty one<\/span> minutes',
'<span>twenty two<\/span> minutes','<span>twenty three<\/span> minutes','<span>twenty four<\/span> minutes',
'<span>twenty five minutes','<span>twenty six minutes','<span>twenty seven minutes',
'<span>twenty eight<\/span> minutes','<span>twenty nine<\/span> minutes','<span> half<\/span>',
'<span>twenty nine<\/span> minutes','<span>twenty eight<\/span> minutes','<span>twenty seven<\/span> minutes',
'<span>twenty six<\/span> minutes','<span>twenty five<\/span> minutes','<span>twenty four<\/span> minutes',
'<span>twenty three minutes','<span>twenty two minutes','<span>twenty one minutes',
'<span>twenty<\/span> minutes','<span>nineteen<\/span> minutes','<span>eighteen<\/span> minutes',
'<span>seventeen<\/span> minutes','<span>sixteen<\/span> minutes','<span>quarter<\/span>',
'<span>fourteen<\/span> minutes','<span>thirteen<\/span> minutes','<span>twelve<\/span> minutes','<span>eleven<\/span> minutes',
'<span>ten<\/span> minutes','<span>nine<\/span> minutes','<span>eight<\/span> minutes','<span>seven<\/span> minutes',
'<span>six<\/span> minutes','<span>five<\/span> minutes','<span>four<\/span> minutes','<span>three<\/span> minutes',
'<span>two<\/span> minutes','<span>one<\/span> minute'
];
hours=[
'<span> twelve<\/span>','<span> one<\/span>','<span> two<\/span>','<span> three<\/span>','<span> four<\/span>' ,
'<span> five<\/span>','<span> six<\/span>','<span> seven<\/span>','<span> eight<\/span>','<span> nine<\/span>',
'<span> ten<\/span>','<span> eleven<\/span>'
];
nmae=[
' at night',' in the morning',' in the afternoon',' in the evening'
];
function past_to(m) {
if (!m || m>30) return "to "
return "past "; // default
}
function oclock (m) {
if (m<=1 || m>59) return " o'clock";
return "" ; // default
}
function init(){
obj=document.getElementById('time');
n=new Date();
h=n.getHours();
m=n.getMinutes();
if(h<24) { period=nmae[3];a=12}
if(h<18) { period=nmae[2];a=12}
if(h<12) { period=nmae[1];a=0}
if(h<6) { period=nmae[0];a=0}
if((h==0)&&(m==0)){
obj.innerHTML='<span>12<\/span> midnight.';
}
else {
if((h==12)&&(m==0)){
obj.innerHTML='<span>12<\/span> noon.';
}
else {
obj.innerHTML="" +minutes[m]+ "<br>" +past_to(m) +hours[h-a] +oclock(m) + "<br>" +period+''; }
}
setTimeout(function(){init()},1000);
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<div id="time"></div>
</body>
</html>
Once again thanks for the help. :)
ApacheTech
06-12-2012, 08:48 PM
The ones that have been posted seem needlessly lengthy. I've made a simple numbers to words clock here, that takes the words for the hours and minutes from a single array, and the array only counts from one to thirty.
EDIT: I've added in the multi-line and font size changes. I'm not sure how you'd want to work "at night" etc. "in the morning" is anywhere from midnight to midday and then from midday to six as the afternoon, six to nine as evening and nine to midnight as at night... maybe. These are the time constraints I've worked to within the code.
UPDATES:
Fixed error when displaying 0 minute times, now displays "one in the morning" as it should.
Added annotation
Added CSS Style that can be amended to an external stylesheet.
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head id='Head1' runat='server'>
<title>JavaScript Time To String Demo</title>
<script type='text/javascript'>
// Amends "s" to plurals
function isPlural(n, s) { if (n > 1) { s+='s'; } return s; }
// Array of numbers as words. For this script, we only need number 1 through 30.
// NOTE: indices are always offset by -1 to their corrosponding string.
var numbersAsWords = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three, ', 'twenty four', 'twenty five', 'twenty six ', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty'];
// Converts the current time to a pre-formatted DIV tag
// containing the time displayed as words.
function timeToString() {
// Gets the current time.
var dTime = new Date();
// Gathers necessary time informaton
var hours = dTime.getHours();
var minutes = dTime.getMinutes();
// Declares and initialise sub-strings
var strConj = 'past';
var amPM = 'in the morning';
// Converts 24 hour time to 12 hour time, using
// the following times as periods of the day:
//*********************************//
// Morning: Midnight to Midday //
// Afternoon: Midday to 6PM //
// Evening: 6PM to 9PM //
// Night: 9PM to Midnight //
//*********************************//
if (hours >= 12) {
hours = hours - 12;
amPM = 'in the afternoon';
if (hours >= 6) {
amPM = 'in the evening';
if (hours >= 9) {
amPM = 'at night';
}
}
}
// Sets minutes to count down rather than up
// after reaching half past the hour.
if (minutes >= 30) {
minutes = 60 - (minutes);
strConj = 'to';
hours++;
}
// Creates the DIV element to return
var displayDiv = document.createElement('div');
displayDiv.setAttribute('id', 'displayDiv');
// Generates HTML Markup to return within displayDiv
var strTimeAsWords = function () {
var s = '';
if (!minutes == 0) {
s += '<p><span class="emphasis">' + numbersAsWords[minutes - 1] + '</span>' + ' ' + isPlural(minutes, 'minute') + ' ' + strConj + '</p>';
}
s += '<p><span class="emphasis">' + numbersAsWords[hours - 1] + '</span>' + ' ' + amPM + '</p>';
return s;
}
// Injects HTML into displayDiv
displayDiv.innerHTML = strTimeAsWords();
// Returns the generated HTML code as a fully formed DIV tag.
return displayDiv;
}
function exampleUsage() {
// Locates and replaces the dummy DIV on the page
var el = document.getElementById('timeDiv');
document.body.replaceChild(timeToString(), el);
// Recursive: Updates every second
setTimeout(exampleUsage(), 1000);
}
// Adds an OnLoad event to the page.
window.addEventListener('load', exampleUsage, false);
</script>
<style type="text/css">
/* TIME TO STRING CLASSES
//-------------------------------------*/
span.emphasis
{
font-size: 2em;
}
</style>
</head>
<body>
<div id='timeDiv'></div>
</body>
</html>
ukstormer
06-19-2012, 06:04 PM
Many thanks to all, between all the help have now got a great script working that looks cool.
ukstormer
06-20-2012, 02:22 PM
The ones that have been posted seem needlessly lengthy. I've made a simple numbers to words clock here, that takes the words for the hours and minutes from a single array, and the array only counts from one to thirty.
EDIT: I've added in the multi-line and font size changes. I'm not sure how you'd want to work "at night" etc. "in the morning" is anywhere from midnight to midday and then from midday to six as the afternoon, six to nine as evening and nine to midnight as at night... maybe. These are the time constraints I've worked to within the code.
UPDATES:
Fixed error when displaying 0 minute times, now displays "one in the morning" as it should.
Added annotation
Added CSS Style that can be amended to an external stylesheet.
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head id='Head1' runat='server'>
<title>JavaScript Time To String Demo</title>
<script type='text/javascript'>
// Amends "s" to plurals
function isPlural(n, s) { if (n > 1) { s+='s'; } return s; }
// Array of numbers as words. For this script, we only need number 1 through 30.
// NOTE: indices are always offset by -1 to their corrosponding string.
var numbersAsWords = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three, ', 'twenty four', 'twenty five', 'twenty six ', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty'];
// Converts the current time to a pre-formatted DIV tag
// containing the time displayed as words.
function timeToString() {
// Gets the current time.
var dTime = new Date();
// Gathers necessary time informaton
var hours = dTime.getHours();
var minutes = dTime.getMinutes();
// Declares and initialise sub-strings
var strConj = 'past';
var amPM = 'in the morning';
// Converts 24 hour time to 12 hour time, using
// the following times as periods of the day:
//*********************************//
// Morning: Midnight to Midday //
// Afternoon: Midday to 6PM //
// Evening: 6PM to 9PM //
// Night: 9PM to Midnight //
//*********************************//
if (hours >= 12) {
hours = hours - 12;
amPM = 'in the afternoon';
if (hours >= 6) {
amPM = 'in the evening';
if (hours >= 9) {
amPM = 'at night';
}
}
}
// Sets minutes to count down rather than up
// after reaching half past the hour.
if (minutes >= 30) {
minutes = 60 - (minutes);
strConj = 'to';
hours++;
}
// Creates the DIV element to return
var displayDiv = document.createElement('div');
displayDiv.setAttribute('id', 'displayDiv');
// Generates HTML Markup to return within displayDiv
var strTimeAsWords = function () {
var s = '';
if (!minutes == 0) {
s += '<p><span class="emphasis">' + numbersAsWords[minutes - 1] + '</span>' + ' ' + isPlural(minutes, 'minute') + ' ' + strConj + '</p>';
}
s += '<p><span class="emphasis">' + numbersAsWords[hours - 1] + '</span>' + ' ' + amPM + '</p>';
return s;
}
// Injects HTML into displayDiv
displayDiv.innerHTML = strTimeAsWords();
// Returns the generated HTML code as a fully formed DIV tag.
return displayDiv;
}
function exampleUsage() {
// Locates and replaces the dummy DIV on the page
var el = document.getElementById('timeDiv');
document.body.replaceChild(timeToString(), el);
// Recursive: Updates every second
setTimeout(exampleUsage(), 1000);
}
// Adds an OnLoad event to the page.
window.addEventListener('load', exampleUsage, false);
</script>
<style type="text/css">
/* TIME TO STRING CLASSES
//-------------------------------------*/
span.emphasis
{
font-size: 2em;
}
</style>
</head>
<body>
<div id='timeDiv'></div>
</body>
</html>
Hi,
I have just found a bug in the code, when the time is 'x' minutes past 12 it says 'undefined' where the twelve should be, any ideas ?
ApacheTech
06-20-2012, 02:39 PM
Try this:
FIND:
if (hours >= 12) {
REPLACE WITH:
if (hours > 12) {
If that doesn't work, revert the correction and try the following instead:
FIND:
hours = hours - 12;
REPLACE WITH:
hours -= 11;
One of these should work. If not, let me know and I'll debug it further.
ukstormer
06-20-2012, 03:47 PM
Try this:
FIND:
if (hours >= 12) {
REPLACE WITH:
if (hours > 12) {
This resolved the initial problem but after 12:29 it goes to 30m to 13
If that doesn't work, revert the correction and try the following instead:
FIND:
hours = hours - 12;
REPLACE WITH:
hours -= 11;
I reverted the code and replaced with this one, 11:30 was OK, 29m to 12 was OK but at 12 it flipped to 1
One of these should work. If not, let me know and I'll debug it further.
nearly there
ApacheTech
06-20-2012, 05:49 PM
Fixed. (hopefully)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Time To String Demo</title>
<script type='text/javascript'>
// Amends "s" to plurals
function isPlural(n, s) { if (n > 1) { s += 's'; } return s; }
// Array of numbers as words. For this script, we only need number 1 through 30.
// NOTE: indices are always offset by -1 to their corrosponding string.
var numbersAsWords = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three, ', 'twenty four', 'twenty five', 'twenty six ', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty'];
// Converts the current time to a pre-formatted DIV tag
// containing the time displayed as words.
function timeToString() {
// Gets the current time.
var dTime = new Date();
// dTime.setHours(23, 59, 59, 999); // DEBUG OVERRIDE
// Gathers necessary time informaton
var hours = dTime.getHours();
var minutes = dTime.getMinutes();
// Declares and initialise sub-strings
var strConj = 'past';
var amPM = 'in the morning';
// Sets minutes to count down rather than up
// after reaching half past the hour.
if (minutes >= 30) {
minutes = 60 - (minutes);
strConj = 'to';
hours++;
}
// Converts 24 hour time to 12 hour time, using
// the following times as periods of the day:
//
// Morning: Midnight to Midday
// Afternoon: Midday to 6PM
// Evening: 6PM to 9PM
// Night: 9PM to Midnight
//
if (hours > 12) {
hours -= 12;
amPM = 'in the afternoon';
if (hours >= 6) {
amPM = 'in the evening';
if (hours >= 9) {
amPM = 'at night';
}
}
} else if (hours == 12) {
amPM = 'in the morning';
} else if (hours == 0) {
hours = 12;
}
// Creates the DIV element to return
var displayDiv = document.createElement('div');
displayDiv.setAttribute('id', 'displayDiv');
// Generates HTML Markup to return within displayDiv
var strTimeAsWords = function () {
var s = '';
if (!minutes == 0) {
s += '<p><span class="emphasis">' + numbersAsWords[minutes - 1] + '</span>' + ' ' + isPlural(minutes, 'minute') + ' ' + strConj + '</p>';
}
s += '<p><span class="emphasis">' + numbersAsWords[hours - 1] + '</span>' + ' ' + amPM + '</p>';
return s;
}
// Injects HTML into displayDiv
displayDiv.innerHTML = strTimeAsWords();
// Returns the generated HTML code as a fully formed DIV tag.
return displayDiv;
}
function exampleUsage() {
// Locates and replaces the dummy DIV on the page
var el = document.getElementById('timeDiv');
document.body.replaceChild(timeToString(), el);
// Recursive: Updates every second
setTimeout(exampleUsage(), 1000);
}
// Adds an OnLoad event to the page.
window.addEventListener('load', exampleUsage, false);
</script>
<style type="text/css">
/* TIME TO STRING CLASSES
//-------------------------------------*/
span.emphasis
{
font-size: 2em;
}
</style>
</head>
<body>
<div id='timeDiv'>
</div>
</body>
</html>
I've tested this with as many awkward times as I can, and it works how it should. Give us a shout if there's anything else.
ukstormer
06-20-2012, 06:50 PM
Many thanks, I have checked as many options all seems right.
As I have said before I am not a java programmer but love playing around and learning through what others have done, my one problem is apart from just running the code in a browser that seems to either work or not is there a program I can use to see where the code is stopping and thus I could better understand java.
Many thanks again fro all you help.
ApacheTech
06-20-2012, 07:54 PM
I use Visual Studio 2010 Professional. It has JavaScript intellisense and a good debugger.
You can download Visual Studio 2010 Express (The free version of what I use) from:
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-web-developer-express
Other people may be able to give advice on other IDEs; VS2010 has everything I could need for Web Development, but it doesn't seem to be widely liked for some reason.
ukstormer
07-03-2012, 07:53 AM
Hi Again,
The code is working great and I have altered the style to a specific text for the main elements and make the actual minutes and hours twice as large as the words around them, the problem now is I want to be able to position the div absolutely, I have done this OK with the Date as Words as that uses the 'document.write' and I have styled the div marker, with the Time as Words not using this method I am not sure how I would position the div.
Many thanks
ajfmrf
07-03-2012, 12:46 PM
Please post a link to what you now have
ukstormer
07-03-2012, 02:26 PM
This is the code so far, I am using an old laptop that has been stripped down and put into a photo frame and I am trying to create an unusual wall clock.
<html>
<head>
<Title> time </title>
<script type='text/javascript'>
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// Amends "s" to plurals
function isPlural(n, s) { if (n > 1) { s+='s'; } return s; }
// Array of numbers as words. For this script, we only need number 1 through 30.
// NOTE: indices are always offset by -1 to their corrosponding string.
var numbersAsWords = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six ', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty'];
// Converts the current time to a pre-formatted DIV tag
// containing the time displayed as words.
function timeToString() {
// Gets the current time.
var Time = new Date();
// Gathers necessary time informaton
var hours = Time.getHours();
var minutes = Time.getMinutes();
// Declares and initialise sub-strings
var strConj = ' past ';
var amPM = 'in the morning';
var oclock = ' '; //default
// Sets minutes to count down rather than up
// after reaching half past the hour.
if (minutes >= 30) {
minutes = 60 - (minutes);
strConj = ' to ';
hours++;
}
if (minutes < 1 || minutes > 59) {
oclock = ' o clock ';
}
// Converts 24 hour time to 12 hour time, using
// the following times as periods of the day:
//*********************************//
// Morning: Midnight to Midday //
// Afternoon: Midday to 6PM //
// Evening: 6PM to 9PM //
// Night: 9PM to Midnight //
//*********************************//
if (hours > 12) {
hours -= 12;
amPM = 'in the afternoon';
if (hours >= 6) {
amPM = 'in the evening';
if (hours >= 9) {
amPM = 'at night';
}
}
} else if (hours == 12) {
amPM = 'in the morning';
} else if (hours == 0) {
hours = 12;
}
// Creates the DIV element to return
var displayDiv = document.createElement('div');
displayDiv.setAttribute('id', 'displayDiv');
// Generates HTML Markup to return within displayDiv
var strTimeAsWords = function () {
var s = '';
if (!minutes == 0) {
s += '<p><span class="emphasis">' + numbersAsWords[minutes - 1] + ' ' + '</span>' + '<span class="basic">' + isPlural(minutes, 'minute') + ' </br> ' + strConj + '</span>' + ' ';
}
s += '<span class="emphasis">' + numbersAsWords[hours - 1] + ' ' + oclock + ' ' + '</span>' + ' ' ;
return s;
}
// Injects HTML into displayDiv
displayDiv.innerHTML = strTimeAsWords();
// Returns the generated HTML code as a fully formed DIV tag.
return displayDiv;
}
function exampleUsage() {
// Locates and replaces the dummy DIV on the page
var el = document.getElementById('timeDiv');
document.body.replaceChild(timeToString(), el);
}
// Adds an OnLoad event to the page.
window.addEventListener('load', exampleUsage, false);
</script>
<style type="text/css">
/* TIME TO STRING CLASSES
//-------------------------------------*/
span.basic
{
font-family:LubalGraph Bd BT;
color:#EA3030;
font-weight:normal;
font-size: 40px;
}
span.emphasis
{
font-family:LubalGraph Bd BT;
color:#EA3030;
font-size: 80px;
}
</style>
<Title> date </title>
<style type="text/css">
body
{
background-image: url(image.jpg);
background-repeat: no-repeat;
background-position: left top;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>date</title>
<script language="Javascript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// -->
var this_date_words_array = new Array ("","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth","twentieth","twenty-first","twenty-second","twenty-third","twenty-fourth","twenty-fifth","twenty-sixth","twenty-seventh","twenty-eighth","twenty-ninth","thirtieth","thirty-first")//date as words
var this_month_name_array = new Array("january","february","march","april","may","june","july","august","september","october","november","december") //month names
var this_date_timestamp=new Date() //get current day-time stamp
var this_date_words = this_date_timestamp.getDate() //extract day of month
var this_month = this_date_timestamp.getMonth() //extract month
var this_year = this_date_timestamp.getFullYear() //extract year
var this_date_string = '<span class="emphasis">' + this_date_words_array[this_date_words] + '</span>' + '</br>' + '<span class="basic">' + ' of ' + '</span>' + '<span class="emphasis">' + this_month_name_array[this_month] + '</span>' + '<span class="basic">' + ' ' + this_year + '</span>' //concat long date string</p>
</script>
</head>
<body onload="JavaScript:timedRefresh(10000);">
<div id="timeDiv"></div>
<div align="right">
<script language="JavaScript">document.write(this_date_string)</script>
</div>
</body>
</html>
I am looking to position the time and date at the bottom of the screen, the time on the left and date on the right.
Thanks
ApacheTech
07-03-2012, 02:53 PM
Don't use align="" to align your code. Use CSS instead. Align has been depreciated and will not validate.
Here is enough to get you started.
CSS:
#dateDiv
{
position: absolute;
left: 0;
bottom: 0;
}
#timeDiv
{
position: absolute;
right: 0;
bottom: 0;
}
Basic HTML:
<div id="dateDiv"></div>
<div id="timeDiv"></div>
ApacheTech
07-03-2012, 02:56 PM
Also, you may want to move all your Javascript and CSS into separate files and link them from the <head> of your page. External CSS and JS files will make your code a lot more manageable.
ukstormer
07-03-2012, 03:52 PM
Thanks for the info, so now created 4 files, HTML, 2 x Javascript and a CSS
clock_date.html
<html>
<head>
<script type="text/javascript" src="time.js"></script>
<script type="text/javascript" src="date.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css" />
<Title> Time and Date as Words </title>
</head>
<body onload="JavaScript:timedRefresh(10000);">
<div id="timeDiv"></div>
<div id="dateDiv"></div>
</body>
</html>
time.js
// Time as Words
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// Amends "s" to plurals
function isPlural(n, s) { if (n > 1) { s+='s'; } return s; }
// Array of numbers as words. For this script, we only need number 1 through 30.
// NOTE: indices are always offset by -1 to their corrosponding string.
var numbersAsWords = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six ', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty'];
// Converts the current time to a pre-formatted DIV tag
// containing the time displayed as words.
function timeToString() {
// Gets the current time.
var Time = new Date();
// Gathers necessary time informaton
var hours = Time.getHours();
var minutes = Time.getMinutes();
// Declares and initialise sub-strings
var strConj = ' past ';
var amPM = 'in the morning';
var oclock = ' '; //default
// Sets minutes to count down rather than up
// after reaching half past the hour.
if (minutes >= 30) {
minutes = 60 - (minutes);
strConj = ' to ';
hours++;
}
if (minutes < 1 || minutes > 59) {
oclock = ' o clock ';
}
// Converts 24 hour time to 12 hour time, using
// the following times as periods of the day:
//*********************************//
// Morning: Midnight to Midday //
// Afternoon: Midday to 6PM //
// Evening: 6PM to 9PM //
// Night: 9PM to Midnight //
//*********************************//
if (hours > 12) {
hours -= 12;
amPM = 'in the afternoon';
if (hours >= 6) {
amPM = 'in the evening';
if (hours >= 9) {
amPM = 'at night';
}
}
} else if (hours == 12) {
amPM = 'in the morning';
} else if (hours == 0) {
hours = 12;
}
// Creates the DIV element to return
var displayDiv = document.createElement('div');
displayDiv.setAttribute('id', 'displayDiv');
// Generates HTML Markup to return within displayDiv
var strTimeAsWords = function () {
var s = '';
if (!minutes == 0) {
s += '<p><span class="emphasis">' + numbersAsWords[minutes - 1] + ' ' + '</span>' + '<span class="basic">' + isPlural(minutes, 'minute') + ' </br> ' + strConj + '</span>' + ' ';
}
s += '<span class="emphasis">' + numbersAsWords[hours - 1] + ' ' + oclock + ' ' + '</span>' + ' ' ;
return s;
}
// Injects HTML into displayDiv
displayDiv.innerHTML = strTimeAsWords();
// Returns the generated HTML code as a fully formed DIV tag.
return displayDiv;
}
function exampleUsage() {
// Locates and replaces the dummy DIV on the page
var el = document.getElementById('timeDiv');
document.body.replaceChild(timeToString(), el);
}
// Adds an OnLoad event to the page.
window.addEventListener('load', exampleUsage, false);
date.js
// Date as Words
var this_date_words_array = new Array ("","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth","twentieth","twenty-first","twenty-second","twenty-third","twenty-fourth","twenty-fifth","twenty-sixth","twenty-seventh","twenty-eighth","twenty-ninth","thirtieth","thirty-first")//date as words
var this_month_name_array = new Array("january","february","march","april","may","june","july","august","september","october","november","december") //month names
var this_date_timestamp=new Date() //get current day-time stamp
var this_date_words = this_date_timestamp.getDate() //extract day of month
var this_month = this_date_timestamp.getMonth() //extract month
var this_year = this_date_timestamp.getFullYear() //extract year
var this_date_string = '<span class="emphasis">' + this_date_words_array[this_date_words] + '</span>' + '</br>' + '<span class="basic">' + ' of ' + '</span>' + '<span class="emphasis">' + this_month_name_array[this_month] + '</span>' + '<span class="basic">' + ' ' + this_year + '</span>' //concat long date string</p>
styles.css
span.basic
{
font-family:LubalGraph Bd BT;
color:#EA3030;
font-weight:normal;
font-size: 40px;
}
span.emphasis
{
font-family:LubalGraph Bd BT;
color:#EA3030;
font-size: 80px;
}
#timeDiv
{
position: absolute;
left: 50px;
top: 50px;
}
#dateDiv
{
position: absolute;
right: 0;
bottom: 0;
}
body
{
background-image: url(image.jpg);
background-repeat: no-repeat;
background-position: left top;
}
I get the time OK but no date, I know the css is linking as I see the background and can change the font size and it updates OK, when I change anything in the timeDiv part of css nothing happens, nearly there and the code is looking neater with separate files but why no date, suspect it is the way the javascript is outputting the results as I can see the link in the timeDiv.
ApacheTech
07-03-2012, 04:46 PM
If it was all working before then this should work. I don't have my test platform VM open atm and it'll take a while to load up, but if you need it thoroughly testing, I can do.
As you seem to know your way around the code now as well, I've taken the training wheels off.
js/scripts.js
function isPlural(n, s) { if (n > 1) { s+='s'; } return s; }
function dateToString() {
var this_date_words_array = new Array ("","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth","sixteenth","seventeenth","eighteenth","nineteenth","twentieth","twenty-first","twenty-second","twenty-third","twenty-fourth","twenty-fifth","twenty-sixth","twenty-seventh","twenty-eighth","twenty-ninth","thirtieth","thirty-first")//date as words
var this_month_name_array = new Array("january","february","march","april","may","june","july","august","september","october","november","december") //month names
var this_date_timestamp=new Date() //get current day-time stamp
var this_date_words = this_date_timestamp.getDate() //extract day of month
var this_month = this_date_timestamp.getMonth() //extract month
var this_year = this_date_timestamp.getFullYear() //extract year
var this_date_string = '<span class="emphasis">' + this_date_words_array[this_date_words] + '</span>' + '</br>' + '<span class="basic">' + ' of ' + '</span>' + '<span class="emphasis">' + this_month_name_array[this_month] + '</span>' + '<span class="basic">' + ' ' + this_year + '</span>'
return this_date_string;
}
function timeToString() {
var numbersAsWords = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six ', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty'];
var Time = new Date();
var hours = Time.getHours();
var minutes = Time.getMinutes();
var strConj = ' past ';
var amPM = 'in the morning';
var oclock = ' '; //default
if (minutes >= 30) {
minutes = 60 - (minutes);
strConj = ' to ';
hours++;
}
if (minutes < 1 || minutes > 59) {
oclock = ' o clock ';
}
/**
* Morning: Midnight to Midday
* Afternoon: Midday to 6PM
* Evening: 6PM to 9PM
* Night: 9PM to Midnight
*/
if (hours > 12) {
hours -= 12;
amPM = 'in the afternoon';
if (hours >= 6) {
amPM = 'in the evening';
if (hours >= 9) {
amPM = 'at night';
}
}
} else if (hours == 12) {
amPM = 'in the morning';
} else if (hours == 0) {
hours = 12;
}
var strTimeAsWords = function () {
var s = '';
if (!minutes == 0) {
s += '<p><span class="emphasis">' + numbersAsWords[minutes - 1] + ' ' + '</span>' + '<span class="basic">' + isPlural(minutes, 'minute') + ' </br> ' + strConj + '</span>' + ' ';
}
s += '<span class="emphasis">' + numbersAsWords[hours - 1] + ' ' + oclock + ' ' + '</span>' + ' ' ;
return s;
}
return strTimeAsWords;
}
function autoRefreshTimeAndDate(timeoutPeriod) {
setTimeout(function () {
var timeDiv = document.getElementById('timeDiv');
var dateDiv = document.getElementById('dateDiv');
timeDiv.innerHTML = timeToString();
dateDiv.innerHTML = dateToString();
},timeoutPeriod);
}
window.addEventListener('load', autoRefreshTimeAndDate(10000), false);
css/styles.css:
/**
* Page Styles
*/
*, html {
padding: 0;
margin: 0;
}
body
{
background-image: url('../img/image.jpg');
background-repeat: no-repeat;
background-position: left top;
}
/**
* Date and Time Syles
*/
#timeDiv
{
position: absolute;
left: 50px;
top: 50px;
}
#dateDiv
{
position: absolute;
right: 0;
bottom: 0;
}
span.basic
{
font-family: "LubalGraph Bd BT";
color: #EA3030;
font-weight :normal;
font-size: 40px;
}
span.emphasis
{
font-family: "LubalGraph Bd BT";
color: #EA3030;
font-size: 80px;
}
/**
* Other CSS Here
*/
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/scripts.js" type="text/javascript"></script>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="timeDiv"></div>
<div id="dateDiv"></div>
</body>
</html>
I've introduced some basic site structure in it. Your site folder and file structure should now be (for purposes of this example):
/
/css/
/css/styles.css
/img/
/img/image.jpg
/js/
/js/scripts.js
/index.html
Setting up a basic structure like this will help you in future to organise your site for when you need some more of the advanced features. I hope this makes sense.
ukstormer
07-03-2012, 05:38 PM
Thanks again for all your help, I have now organised all the files in the correct folders.
Regards the styling I have never had any styling regards the div position working, the only style I was using was the div align, will keep on playing around and reading my books.
Also had to go back to this to get the date to show
<div>
<script language="JavaScript">document.write(this_date_string)</script>
</div>
I know it is to do with the document write and getting it to reference the div ID.
Onwards and Upwards as they say ;)
ApacheTech
07-03-2012, 05:49 PM
I've just had a detailed look through the Javascript and I've found some errors.
For this demo I've introduced JQuery. It's a library of Javascript functions that make coding JS a lot easier.
js/scripts.js:
function isPlural(n, s) { if (n > 1) { s+='s'; } return s; }
function dateToString() {
var aDates = ["","first","second","third","fourth","fifth","sixth","seventh","eighth",
"ninth","tenth","eleventh","twelfth","thirteenth","fourteenth","fifteenth",
"sixteenth","seventeenth","eighteenth","nineteenth","twentieth","twenty-first",
"twenty-second","twenty-third","twenty-fourth","twenty-fifth","twenty-sixth",
"twenty-seventh","twenty-eighth","twenty-ninth","thirtieth","thirty-first"];
var aMonths = ["january","february","march","april","may","june","july","august","september","october","november","december"];
var dDate = new Date();
var iDate = dDate.getDate();
var iMonth = dDate.getMonth();
var iYear = dDate.getFullYear();
var sDate = '<span class="emphasis">' + aDates[iDate] + '</span>' + '<br />' + '<span class="basic">' + ' of ' + '</span>' + '<span class="emphasis">' + aMonths[iMonth] + '</span>' + '<span class="basic">' + iYear + '</span>'
return sDate;
}
function timeToString() {
var aNumbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six ', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty'];
var dTime = new Date();
var iHours = Time.getHours();
var iMinutes = Time.getMinutes();
var sConj = ' past ';
var sAmPm = 'in the morning';
var sOClock = ' '; //default
if (iMinutes >= 30) {
iMinutes = 60 - (minutes);
sConj = ' to ';
iHours++;
}
if (iMinutes < 1 || iMinutes > 59) {
sOClock = " o'clock ";
}
/**
* Morning: Midnight to Midday
* Afternoon: Midday to 6PM
* Evening: 6PM to 9PM
* Night: 9PM to Midnight
*/
if (iHours > 12) {
iHours -= 12;
sAmPm = 'in the afternoon';
if (iHours >= 6) {
sAmPm = 'in the evening';
if (iHours >= 9) {
sAmPm = 'at night';
}
}
} else if (iHours == 12) {
sAmPm = 'in the morning';
} else if (iHours == 0) {
iHours = 12;
}
var sTime = function () {
var s = '';
if (!iMinutes == 0) {
s += '<p><span class="emphasis">' + aNumbers[iMinutes - 1] + ' ' + '</span>' + '<span class="basic">' + isPlural(iMinutes, 'minute') + ' </br> ' + sConj + '</span>' + ' ';
}
s += '<span class="emphasis">' + aNumbers[iHours - 1] + sOClock + '</span>';
return s;
}
return sTime;
}
jQuery(function($) {
setInterval(function () {
$("timeDiv").html(timeToString());
$("dateDiv").html(dateToString());
},10000);
});
And then you'll need to add the link to the JQuery library. Here, I'm using a fail-safe method for the script above, and I'm letting Google host the JQuery library.
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="js/scripts.js" type="text/javascript"></script>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="timeDiv"></div>
<div id="dateDiv"></div>
</body>
</html>
The JQuery link.src here may seem weird. It's what's known as a "protocol-less" url. This means that it will detect if you are using HTTP or HTTPS and use the same protocol.
The CSS file will remain the same as the previous post, as will the site structure.
This is a much better way of doing it than the previous method.
ukstormer
07-03-2012, 06:04 PM
Many thanks for the quick response, I will have a look at it tomorrow.
ApacheTech
07-03-2012, 06:16 PM
I hope it's all right now. Bug fixing can be a nightmare.
ukstormer
07-04-2012, 08:55 AM
I hope it's all right now. Bug fixing can be a nightmare.
I don't know if I am missing something but the code above does not work at all, I only see the background image ?
bernie1227
07-04-2012, 12:32 PM
Same for me, no clock in site
ukstormer
07-04-2012, 12:52 PM
I went back to my original code and regards the date added the following in the HTML
<body onload="JavaScript:timedRefresh(10000);">
<div id="timeDiv"></div>
<div id="dateDiv"> //new line
<script language="JavaScript">document.write(this_date_string)</script>
</div>
</body>
I can now adjust the absolute position of the date.
ukstormer
07-04-2012, 02:46 PM
Thought I would show what I am trying to achieve. This is an old laptop and I am now stripping it down and going to mount the screen in a frame to make a wall clock, next would be to maybe make a photo gallery as the background rather than just 1 image. Got everything working but some of the code is poor, working on that though. System is Linux and chrome (great as natively supports full screen mode)
ApacheTech
07-04-2012, 08:27 PM
Sorry. Stupid rookie mistake.
js/scripts.js:
function isPlural(n, s) { if (n > 1) { s += 's'; } return s; }
function dateToString() {
var aDates = ["", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth",
"ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth",
"sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth", "twenty-first",
"twenty-second", "twenty-third", "twenty-fourth", "twenty-fifth", "twenty-sixth",
"twenty-seventh", "twenty-eighth", "twenty-ninth", "thirtieth", "thirty-first"];
var aMonths = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var dDate = new Date();
var iDate = dDate.getDate();
var iMonth = dDate.getMonth();
var iYear = dDate.getFullYear();
var sDate = '<span class="emphasis">' + aDates[iDate] + '</span>' + '<br />' + '<span class="basic">' + ' of ' + '</span>' + '<span class="emphasis">' + aMonths[iMonth] + '</span>' + '<span class="basic">' + iYear + '</span>'
return sDate;
}
function timeToString() {
var aNumbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six ', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty'];
var dTime = new Date();
var iHours = dTime.getHours();
var iMinutes = dTime.getMinutes();
var sConj = ' past ';
var sAmPm = 'in the morning';
var sOClock = ' '; //default
if (iMinutes >= 30) {
iMinutes = 60 - (minutes);
sConj = ' to ';
iHours++;
}
if (iMinutes < 1 || iMinutes > 59) {
sOClock = " o'clock ";
}
/**
* Morning: Midnight to Midday
* Afternoon: Midday to 6PM
* Evening: 6PM to 9PM
* Night: 9PM to Midnight
*/
if (iHours > 12) {
iHours -= 12;
sAmPm = 'in the afternoon';
if (iHours >= 6) {
sAmPm = 'in the evening';
if (iHours >= 9) {
sAmPm = 'at night';
}
}
} else if (iHours == 12) {
sAmPm = 'in the morning';
} else if (iHours == 0) {
iHours = 12;
}
var sTime = function () {
var s = '';
if (!iMinutes == 0) {
s += '<p><span class="emphasis">' + aNumbers[iMinutes - 1] + ' ' + '</span>' + '<span class="basic">' + isPlural(iMinutes, 'minute') + ' </br> ' + sConj + '</span>' + ' ';
}
s += '<span class="emphasis">' + aNumbers[iHours - 1] + sOClock + '</span>';
return s;
}
return sTime;
}
$(document).ready(function () {
$("#timeDiv").html(timeToString());
$("#dateDiv").html(dateToString());
setInterval(function () {
$("#timeDiv").html(timeToString());
$("#dateDiv").html(dateToString());
}, 10000);
});
ukstormer
07-05-2012, 11:36 AM
Found a bug, works from 0 to 29 minutes but then locks on 29, doing an F5 refresh loses the date and time, once the time gets to O'clock the clock and date re-appear again until 29.
Don't know if this was a test for me 'ApacheTech' to make sure I am paying attention and learning but looked through the code and sorted it out, the error is as follows
if (iMinutes >= 30) {
iMinutes = 60 - (minutes);
sConj = ' to ';
iHours++;
Should read
if (iMinutes >= 30) {
iMinutes = 60 - (iMinutes);
sConj = ' to ';
iHours++;
Will keep an eye on it over the next few hours but pretty sure it should be OK now.
ajfmrf
07-05-2012, 06:08 PM
Okay,so what is the whole code look like now.After all adjustments?
I am just curious lol
ukstormer
07-06-2012, 06:44 AM
OK, been up and running now with no apparent bugs, below are the final scripts, the only changes I have made are I downloaded the jquery.min.js and re referenced it in the HTML file and you will have to add your own image at 1024x768.
A big thank you to 'ApacheTech' for all your help and to others that have contributed along the way on this thread and the other thread 'date to words'.
Index.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/scripts.js" type="text/javascript"></script>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="timeDiv"></div>
<div id="dateDiv"></div>
</body>
</html>
Scripts.js
function isPlural(n, s) { if (n > 1) { s += 's'; } return s; }
function dateToString() {
var aDates = ["", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth",
"ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth",
"sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth", "twenty-first",
"twenty-second", "twenty-third", "twenty-fourth", "twenty-fifth", "twenty-sixth",
"twenty-seventh", "twenty-eighth", "twenty-ninth", "thirtieth", "thirty-first"];
var aMonths = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var dDate = new Date();
var iDate = dDate.getDate();
var iMonth = dDate.getMonth();
var iYear = dDate.getFullYear();
var sDate = '<span class="emphasis">' + aDates[iDate] + '</span>' + '<br />' + '<span class="basic">' + ' of ' + '</span>' + '<span class="emphasis">' + aMonths[iMonth] + '</span>' + '<span class="basic">' + iYear + '</span>'
return sDate;
}
function timeToString() {
var aNumbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty one', 'twenty two', 'twenty three', 'twenty four', 'twenty five', 'twenty six ', 'twenty seven', 'twenty eight', 'twenty nine', 'thirty'];
var dTime = new Date();
var iHours = dTime.getHours();
var iMinutes = dTime.getMinutes();
var sConj = ' past ';
var sAmPm = 'in the morning';
var sOClock = ' '; //default
if (iMinutes >= 30) {
iMinutes = 60 - (iMinutes);
sConj = ' to ';
iHours++;
}
if (iMinutes < 1 || iMinutes > 59) {
sOClock = " o'clock ";
}
/**
* Morning: Midnight to Midday
* Afternoon: Midday to 6PM
* Evening: 6PM to 9PM
* Night: 9PM to Midnight
*/
if (iHours > 12) {
iHours -= 12;
sAmPm = 'in the afternoon';
if (iHours >= 6) {
sAmPm = 'in the evening';
if (iHours >= 9) {
sAmPm = 'at night';
}
}
} else if (iHours == 12) {
sAmPm = 'in the morning';
} else if (iHours == 0) {
iHours = 12;
}
var sTime = function () {
var s = '';
if (!iMinutes == 0) {
s += '<p><span class="emphasis">' + aNumbers[iMinutes - 1] + ' ' + '</span>' + '<span class="basic">' + isPlural(iMinutes, 'minute') + ' </br> ' + sConj + '</span>' + ' ';
}
s += '<span class="emphasis">' + aNumbers[iHours - 1] + sOClock + '</span>';
return s;
}
return sTime;
}
$(document).ready(function () {
$("#timeDiv").html(timeToString());
$("#dateDiv").html(dateToString());
setInterval(function () {
$("#timeDiv").html(timeToString());
$("#dateDiv").html(dateToString());
},1000);
});
Styles.css
/**
* Page Styles
*/
*, html {
padding: 0px;
margin: 0px;
}
body
{
background-image: url(../img/image.jpg);
background-repeat: no-repeat;
background-position: left top;
}
/**
* Date and Time Syles
*/
#timeDiv
{
position: absolute;
left: 5px;
top: 610px;
}
#dateDiv
{
position: absolute;
left: 610px;
top: 610px;
}
span.basic
{
font-family: "LubalGraph Bd BT";
color: #EA3030;
font-weight :normal;
font-size: 30px;
}
span.emphasis
{
font-family: "LubalGraph Bd BT";
color: #EA3030;
font-size: 60px;
}
/**
* Other CSS Here
*/
As this project is aimed at creating an unusual wall clock and at present has a static single image I can see that the next step would be to make the background a photo gallery but I am now going to make the frame for the screen and components.
Any other ideas would be welcomed.
Enjoy
bernie1227
07-06-2012, 07:58 AM
sounds like an upclass electronic picture frame :p
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.