Jonathan Clark
Posts: 0
Joined: Thu Mar 14, 2013 5:25 pm

Trouble with date time calculations on _updatedAt field

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!

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Trouble with date time calculations on _updatedAt field

Where exactly is there is a 3 hour difference?

Jonathan Clark
Posts: 0
Joined: Thu Mar 14, 2013 5:25 pm

Trouble with date time calculations on _updatedAt field

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)

Jonathan Clark
Posts: 0
Joined: Thu Mar 14, 2013 5:25 pm

Trouble with date time calculations on _updatedAt field

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Trouble with date time calculations on _updatedAt field

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

Jonathan Clark
Posts: 0
Joined: Thu Mar 14, 2013 5:25 pm

Trouble with date time calculations on _updatedAt field

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.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Trouble with date time calculations on _updatedAt field

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.

Jonathan Clark
Posts: 0
Joined: Thu Mar 14, 2013 5:25 pm

Trouble with date time calculations on _updatedAt field

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Trouble with date time calculations on _updatedAt field

Thank you for updating :)

Return to “Issues”