JavaScript Date Objects: Universal Coordinated Time - Introducing One of the Most Asinine Functions Ever
(Page 3 of 4 )
If you love the date 01/01/1970 -- and let's face it, who doesn't? -- and feel the insane need to know how many milliseconds have passed since that vainglorious date, JavaScript has a function that will knock your socks off, slap your momma, and make the cows quit their European traveling and come back home.
The parse() function people, in all its momentous beauty:
<html>
<body>
<script type="text/javascript">
var d = Date.parse("Dec 15 2007");
document.write("Behold! ")
document.write(d);
document.write(" milliseconds have passed between Jan 01, 1970-Dec 15 2007!")
</script>
</body>
</html>
The result:
Behold! 1197694800000 milliseconds have passed between Jan 01, 1970-Dec 15 2007!
Here is another example, where we make the parse() function a little more useful by tallying up the amount of minutes, hours, days, and years between 01/01/1970 and 12/15/2007:
<html>
<body>
<script type="text/javascript">
var minutes = 1000 * 60;
var hours = minutes * 60;
var days = hours * 24;
var years = days * 365;
var toe = Date.parse("Dec 15, 2007");
document.write("Here is a list of minutes, hours, days, and years");
document.write("<br />")
document.write("That have passed between 01/01/1970 and 12/15/1007");
document.write("<br />")
document.write("<br />")
document.write("Minutes that have passed: " + toe/minutes);
document.write("<br />")
document.write("Hours that have passed: " + toe/hours);
document.write("<br />")
document.write("Days that have passed: " + toe/days);
document.write("<br />")
document.write("Years that have passed: " + toe/years);
</script>
</body>
</html>
And once more, the result:
Here is a list of minutes, hours, days, and years
That have passed between 01/01/1970 and 12/15/1007
Minutes that have passed: 19961580
Hours that have passed: 332693
Days that have passed: 13862.208333333334
Years that have passed: 37.97865296803653
Next: Using the UTC() Function >>
More JavaScript Articles
More By James Payne