Pages

Tuesday, January 3, 2012

Convert to milliseconds when adding time in JavaScript

To keep time uniform, JavaScript represents it as the number of milliseconds since midnight January 1st, 1970 GMT. As a result, whenever you want to mathematically manipulate a time, you'll need to convert everything to milliseconds.

For example, the following code illustrates how to add eight hours to the current time:

var dtHere = new Date();
var oneDayFromNow = dtHere.getTime() + (8 * 60 * 60 * 1000);
dtHere.setTime(oneDayFromNow);


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.