Mark the time to the nearest date

I need to group a bunch of items in my web application by date.

Each item has an exact timestamp, for example. 1417628530199 . I use Moment.js and its occasional "to convert these raw timestamps to readable dates like 2 Days Ago . Then I want to use a readable date as a header for a group of items created on the same date.

The problem is that the original timestamps are too specific - for two elements created on the same date, but every minute each will have a unique timestamp. So I get a heading for 2 Days Ago with the first item below it, then another heading for 2 Days Ago with the second item below it, etc.

What is the best way to round raw timestamps to the nearest date so that all items created on the same date have the same timestamp and therefore can be grouped together?

+11
source share
6 answers

Try the following:

 Date.prototype.formatDate = function() { var yyyy = this.getFullYear().toString(); var mm = (this.getMonth()+1).toString(); var dd = this.getDate().toString(); return yyyy + (mm[1]?mm:"0"+mm[0]) + (dd[1]?dd:"0"+dd[0]); }; var utcSeconds = 1417903843000, d = new Date(0); d.setUTCSeconds(Math.round( utcSeconds / 1000.0)); var myTime = (function(){ var theTime = moment(d.formatDate(), 'YYYYMMDD').startOf('day').fromNow(); if(theTime.match('hours ago')){ return 'Today'; } return theTime; })(); alert( myTime ); 

http://jsfiddle.net/cdn5rvck/4/

+1
source

Well, using js, you can do:

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

Edit:

After checking a few methods, this one seems to be faster:

 function roundDate(timeStamp){ timeStamp -= timeStamp % (24 * 60 * 60 * 1000);//subtract amount of time since midnight timeStamp += new Date().getTimezoneOffset() * 60 * 1000;//add on the timezone offset return new Date(timeStamp); } 

You can check the speed difference here: http://jsfiddle.net/juvian/3aqmhn2h/

+24
source

Using Moment.js, you can use the following code to round everyone up to the start of the day:

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

You can learn more about startOf() in docs .

+10
source

Just create a new date from the existing one, using only the year, month and date. Add half a day to make sure this is the closest date.

 var offset = new Date(Date.now() +43200000); var rounded = new Date(offset .getFullYear(),offset .getMonth(),offset .getDate()); console.log(new Date()); console.log(rounded); 

Since this seems to take up little space, it may also be useful to extend the prototype to include it in the Date “class”.

 Date.prototype.round = function(){ var dateObj = new Date(+this+43200000); return new Date(dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate()); }; console.log(new Date().round()); 

Minimization:

 Date.prototype.round = function(){var d = new Date(+this+43200000);return new Date(d.getFullYear(), d.getMonth(), d.getDate());}; 
+4
source
 function roundDownDate(date) { if (typeof date !== "object" || !date.getUTCMilliseconds) { throw Error("Arg must be a Date object."); } var offsetMs = date.getTimezoneOffset() * 60 * 1000, oneDayMs = 24 * 60 * 60 * 1000; return new Date(Math.floor((date.getTime() - offsetMs) / oneDayMs) * oneDayMs + offsetMs); }; 

This should work pretty quickly.

0
source
 function getDateOfTimeStamp(time) { var originTime = 0; var offsetOriginTime = originTime + new Date().getTimezoneOffset() * 60 * 1000; var timeSinceOrigin = time - offsetOriginTime; var timeModulo = timeSinceOrigin % (24 * 60 * 60 * 1000); var normalizedTime = time - timeModulo; console.log(new Date(normalizedTime) ,new Date(time)); return normalizedTime; } 

This worked for my project. Pure mathematics, no string manipulation is required, no external library is required, so it is very fast.

You can try by copying the above function into the javascript console, and then run normalizeTimeToDate(Date.now())

0
source

Source: https://habr.com/ru/post/979192/


All Articles