Cody Blue
Posts: 0
Joined: Sun Aug 25, 2013 2:11 am

Accurate time stamping for events

I intend to accurately list how long ago was a backend record updated. I am fetching _updatedAt timestamp from Appery database, and doing the following:

var fromNowDate = moment(_updatedAt, 'YYYY-MM-DD HH:mm:ss.SSS').fromNow();

http://momentjs.com/docs/

However, I am noticing that if an event was just updated, it shows up as -- updated in 7 hrs -- as returned by fromNowDate above. Can you suggest a way for me to accurately timestamp events. I am intending for a min level accuracy. e.g -- updated 2 mins ago -- for an event that was updated 2 mins prior.

Many thanks.

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Accurate time stamping for events

Hi Cody,

I've checked this code and it seems to be ok.

See details here http://prntscr.com/4abfma/direct

So please - out to console "_updatedAt" field and "new Date()".

I guess it could be a time zone issue.

If so you should convert your compare date to local time zone date.

Regards.

Cody Blue
Posts: 0
Joined: Sun Aug 25, 2013 2:11 am

Accurate time stamping for events

Hi Yurii,

Thanks very much for looking into the matter.

It does seem like a time zone issue (I am in Pacific/US time zone): I can hardcode a correction to work in current time zone, but it wouldn't work for someone else in a different one.

Would you have suggestions to do the correction for a universal approach? How I can I convert the backend time to a 'universal' time? It would be ideal if I had the offset parameter (ZZ: see below) but the Appery time stamp from backend doesn't return it making it hard to retried a 'universal' stamp.
http://momentjs.com/docs/

Thanks in advance.

Regards.

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Accurate time stamping for events

Hi Cody,

Yeap, it's possible to implement.

Here is solution for you:

pre

//Replace this "2014-08-12 23:19:24.495" value with dinamic value you have.
var stringDate = "2014-08-12 23:19:24.495";

var arrDate = stringDate.split(/[- :]/);
var date = new Date(arrDate[0], arrDate[1]-1, arrDate[2], arrDate[3], arrDate[4]);

//Convert date to UTC
var localDate = new Date(date.getTime() - (date.getTimezoneOffset() * 60000));

var fromNow = moment(localDate).fromNow();

alert("from now = " + fromNow);

/pre

Regards.

Return to “Issues”