themactep.com

A home of miscellaneous projects by Paul Philippov.

JavaScript Date class methods (Swatch Internet Time)

Date.toInternetTime()

Convert any Date instance to .beats value with .toInternetTime(n) method, where n controls precision of the result, limiting the number of digits after decimal point (centibeats) and rounding the value accordingly.

<script src="/js/ppds.date.js"></script>
<script>
var date = new Date;                 // "Fri Sep 11 2009 03:43:59 GMT+0700 (NOVT)"
var beats = date.toInternetTime();   // "@906"
var beats = date.toInternetTime(1);  // "@905.5"
var beats = date.toInternetTime(2);  // "@905.54"
</script>

Date.getDayOfYear()

.getDayOfYear() method does not directly relate to Swatch concept of the Internet Time, but I thought it’s a nice addition. Now you can get a number of the day in a year and, say, celebrate Computer Programmers Day which is either September 12th or 13th depending on is this a leap-year or not, but always the 256-th day of a year.

<script src="/js/ppds.date.js"></script>
<script>
var date = new Date;                 // "Fri Sep 11 2009 03:43:59 GMT+0700 (NOVT)"
var beats = date.getDayOfYear();     // "254"
</script>

Source code

Download ppds.date.js (0.7 KB)

License

WTFPL License