3 April, 2014
0 Comments
1 category
Be aware, that dates with different daylight saving times, don’t differ by 24 hours per day, but 24 * days + 1 hour or 24 * days – 1 hour:
var app = (function () { var self = this; // The "main" entry point for this application. self.start = function () { console.log("Document ready!"); self.daylightSavingsTest(); }; self.daylightSavingsTest = function () { // Notes (in the netherlands): // "Winter time" refers to "standard time". // "Summer time" refers to "standard time" + 1 hour. // Time changes at sunday [2014-03-30: 02:00:00.000]. Changes to [2014-03-30: 03:00:00.000]. var d1 = new Date(2014, 1, 1, 0, 0, 0, 0); // '2014-02-01: 00:00:00.000'; var d2 = new Date(2014, 1, 3, 0, 0, 0, 0); // '2014-02-03: 00:00:00.000'; // [getTime()], returns the number of milliseconds since [1970-01-01 00:00:00] (winter / standard time). var t1 = d1.getTime(); var t2 = d2.getTime(); // When all daylight saving times of 2 dates are the same, // you can calculate the difference in days between 2 dates with parseInt((t2 - t1) / (24 * 60 * 60 * 1000)): var days = parseInt((t2 - t1) / (24 * 60 * 60 * 1000)); console.log(days); // Outputs: 2 // When all daylight saving times of 2 dates are NOT the same, // you can't calculate the difference in days between 2 dates with parseInt((t2 - t1) / (24 * 60 * 60 * 1000)): d1 = new Date(2014, 2, 30, 0, 0, 0, 0); // '2014-03-30: 00:00:00.000'; d2 = new Date(2014, 3, 1, 0, 0, 0, 0); // '2014-04-01: 00:00:00.000'; t1 = d1.getTime(); t2 = d2.getTime(); days = parseInt((t2 - t1) / (24 * 60 * 60 * 1000)); console.log(days); // Outputs: 1 // You can fix this by using Math.round: days = Math.round((t2 - t1) / (24 * 60 * 60 * 1000)); console.log(days); // Outputs: 2 // Or just use moment.js!!!! }; return self; })(); // Start the application. app.start();
Tags: Javascript
Category: Uncategorized