Round a timestamp to the nearest date

Solution 1:

Well, using js you can do:

var d = new Date(1417628530199);
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);

A shorter way of writing it is d.setHours(0, 0, 0, 0);

Solution 2:

Using Moment.js, you can use the following code to round everything to the beginning of the day:

moment().startOf('day').toString();
// -> Prints out "Fri Dec 05 2014 00:00:00 GMT-0800"

You can read more about startOf() in the docs.

Solution 3:

Here is a clean way to get just the date in one line with no dependencies:

let d = new Date().setHours(0, 0, 0, 0);