Date and time properties
Hey, buddy -- got the date and time?
<script>
<!--
thedate=new Date() ;
document.write("Sure! It's : ", thedate.toLocaleString()) ;
//-->
</script>
The toLocaleString() method returns the local date and time.
Come again?
<script>
<!--
thedate=new Date() ;
thedow=thedate.getDay() ;
thefullyear=thedate.getFullYear() ;
themonth=thedate.getMonth() ;
theday=thedate.getDate() ;
thetime=thedate.getTime() ;
thehours=thedate.getHours() ;
theminutes=thedate.getMinutes() ;
theseconds=thedate.getSeconds() ;
themilliseconds=thedate.getMilliseconds() ;
document.write("The day of the week is: ", thedow, "<br>") ;
document.write("The year is: ", thefullyear, "<br>") ;
document.write("The month is: ", themonth, "<br>") ;
document.write("The day is: ", theday, "<br>") ;
document.write("The time is: ", thetime, "<br>") ;
document.write("The hour is: ", thehours, "<br>") ;
document.write("The minute is: ", theminutes, "<br>") ;
document.write("The second is: ", theseconds, "<br>") ;
document.write("The millisecond is: ", themilliseconds) ;
//-->
</script>
Not very useful for display, unfortunately.
So what's the date?
<script language="JavaScript" type="text/javascript">
<!--
thedate=new Date() ;
theyear=thedate.getFullYear();
themonthnum=thedate.getMonth();
theday=thedate.getDate();
var themonthtxt ;
switch(themonthnum) {
case 0: themonthtxt='January' ; break ;
case 1: themonthtxt='February' ; break ;
case 2: themonthtxt='March' ; break ;
case 3: themonthtxt='April' ; break ;
case 4: themonthtxt='May' ; break ;
case 5: themonthtxt='June' ; break ;
case 6: themonthtxt='July' ; break ;
case 7: themonthtxt='August' ; break ;
case 8: themonthtxt='September' ; break ;
case 9: themonthtxt='October' ; break ;
case 10: themonthtxt='November' ; break ;
case 11: themonthtxt='December' ; break ;
default: themonthtxt='Unknown month' ; break ;
}
document.write("It's ", themonthtxt, ' ', theday, ', ', theyear) ;
//-->
</script>
A little work makes a much more readable date.
So how long is it until Christmas?
<script>
<!--
today = new Date() ;
christmas = new Date() ;
christmas.setMonth(11) ;
christmas.setDate(25) ;
// If Christmas hasn't already passed, compute the number of
// milliseconds between now and Christmas, then convert this
// to a number of days and print a message.
if (today.getTime() < christmas.getTime()) {
differencems = christmas.getTime() - today.getTime() ;
differencedays = Math.floor(differencems / (1000 * 60 * 60 * 24)) ;
document.write('Only ' + differencedays + 'days (' + differencems.toLocaleString() + ' milliseconds) until Christmas!');
}
else {document.write('Christmas has come and gone. Wait until next year!')
}
//-->
</script>