Page 1 of 1

Trouble with date time calculations on _updatedAt field

Posted: Sun Mar 17, 2013 11:18 pm
by Jonathan Clark

Hey,
I'm having a problem with some date calculations and can't figure out whats going on. I have a service that returns an array of responses. Each response contains fields from the users collection including the _updatedAtField. I have the value of that field mapped to a label on the page. I then have the following javascript code processing the value:

var curTime = new Date();
var curTimesec = Date.parse(curTime);

var updateTime = new Date(value);
var updateTimesec = Date.parse(updateTime);

var dif = curTimesec - updateTimesec;

Code: Select all

 if (dif < 1000){ 
     return '1 second ago'; 
 } 

 else if (dif < 60000){ 
     var sec = dif/1000; 
     return Math.round(sec) + ' seconds ago'; 
 } 

 else if (dif < 3600000){ 
     var min = dif/60000; 
     if (min < 86400000){ 
     var hours = dif/3600000; 
     if (hours< 2){ 
          return Math.round(days) + ' day ago'; 
     } 
     else{ 
          return Math.round(days) + ' days ago'; 
     } 

 } 

Everything seems to work correctly except the time difference is three hours off. I've tried everything I can think of but not able to get this to work correctly. Any advice would be greatly appreciated. THANKS!


Trouble with date time calculations on _updatedAt field

Posted: Mon Mar 18, 2013 3:00 am
by maxkatz

Where exactly is there is a 3 hour difference?


Trouble with date time calculations on _updatedAt field

Posted: Mon Mar 18, 2013 3:20 am
by Jonathan Clark

I perform an update on one the records and then immediately pull the _updatedatedat field (value) subract that from time now and I'm getting a 3 hour difference. It should be no difference (or just a couple seconds ago)


Trouble with date time calculations on _updatedAt field

Posted: Mon Mar 18, 2013 3:24 am
by Jonathan Clark

Fyi, all I'm really wanting to do is display how long its been since a specific record was last updated.


Trouble with date time calculations on _updatedAt field

Posted: Mon Mar 18, 2013 10:35 am
by Kateryna Grynko

Hi Jonathan,

The problem is probably in different time zones. In Tiggzi Database, time is stored as PDT timezone.

You should replace the following line:
codevar updateTime = new Date(value);/code
with:
codevar updateTime = new Date(value + " PDT");/code


Trouble with date time calculations on _updatedAt field

Posted: Mon Mar 18, 2013 6:38 pm
by Jonathan Clark

O.K. so I may have been a little confused then.... I read this in the documentation:

The Date type value is a string which contains a UTC timestamp stored in ISO 8601 format with millisecond precision: YYYY-MM-DDTHH:MM:SS.MMMZ.

So I was expecting zulu time. I may be really confused about the way times are stored, but just wanted to point this out in case it was an error in the documentation.


Trouble with date time calculations on _updatedAt field

Posted: Mon Mar 18, 2013 8:10 pm
by Maryna Brodina

Hello! Yes, you are right. It's confusing... We'll continue researching and check what we can do here - change documentation or this is some kind of bug which we should fix.


Trouble with date time calculations on _updatedAt field

Posted: Fri Mar 22, 2013 2:24 pm
by Jonathan Clark

Just to update, adding ( + "PDT") worked perfectly. Time calculations are all displaying correctly


Trouble with date time calculations on _updatedAt field

Posted: Fri Mar 22, 2013 3:04 pm
by Kateryna Grynko

Thank you for updating :)