Page 1 of 1

Accurate time stamping for events

Posted: Wed Aug 06, 2014 11:45 pm
by Cody Blue

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.


Accurate time stamping for events

Posted: Thu Aug 07, 2014 1:13 am
by Yurii Orishchuk

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.


Accurate time stamping for events

Posted: Tue Aug 12, 2014 3:14 am
by Cody Blue

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.


Accurate time stamping for events

Posted: Tue Aug 12, 2014 11:38 pm
by Yurii Orishchuk

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.