JSFiddle - React, Tailwind, and code Playground
by dreamyguy
JavaScript
var roundedDate1 = function(timestamp) {
var t = new Date(timestamp);
t.setHours(0);
t.setMinutes(0);
t.setSeconds(0);
t.setMilliseconds(0);
return t;
};
var roundedDate2 = function(timestamp) {
var t = new Date(timestamp);
return new Date(t.getFullYear(), t.getMonth(), t.getDate(), 0, 0, 0, 0)
};
var roundedDate3 = function(timestamp) {
timestamp -= timestamp % (24 * 60 * 60 * 1000); // substract amount of time since midnight
timestamp += new Date().getTimezoneOffset() * 60 * 1000; // add the timezone offset
return new Date(timestamp);
};
var timestamp = 1417628530199;
console.log('1 ' + roundedDate1(timestamp));
console.log('2 ' + roundedDate2(timestamp));
console.log('3 ' + roundedDate3(timestamp));
// output
// 1 Wed Dec 03 2014 00:00:00 GMT+0100 (CET)
// 2 Wed Dec 03 2014 00:00:00 GMT+0100 (CET)
// 3 Tue Dec 02 2014 23:00:00 GMT+0100 (CET)