javascript - Round a timestamp to the nearest date -
i need group bunch of items in web app date created.
each item has exact timestamp, e.g. 1417628530199
. i'm using moment.js , "time now" feature convert these raw timestamps nice readable dates, e.g. 2 days ago
. want use readable date header group of items created on same date.
the problem raw timestamps specific - 2 items created on same date minute apart each have unique timestamp. header 2 days ago
first item underneath, header 2 days ago
second item underneath, etc.
what's best way round raw timestamps nearest date, items created on same date have exact same timestamp , can grouped together?
try this:
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 );
Comments
Post a Comment