9

i've looking around how to do this and i found a lot of examples with complicated code. Im using this:

var time1 = new Date();
var time1ms= time1.getTime(time1); //i get the time in ms  

then i do this in other part of the code

var time2 = new Date();
var time2ms= time2.getTime(time2); 

and finnally:

var difference= time2ms-time1ms;
var lapse=new Date(difference);  
label.text(lapse.getHours()+':'+lapse.getMinutes()+':'+lapse.getSeconds());

This works great, except for one issue, the hours it gaves me are always +1 so i have to add to the code (time.getHours()-1) otherwise it gaves me one hour more....

I think is an easier way to do it than all the other examples around... but i still dont understand why i need to add '-1' in order to have the correct lapse.

THanks!!!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
pistoleta
  • 121
  • 1
  • 1
  • 7

2 Answers2

15

The problem is your timezone.

When you do new Date(difference), you're creating a Date object that represent the moment exatcly difference milliseconds after January 1st, 1970. When you do lapse.getHours() your timezone is used in the computation. You cannot modify your timezone via Javascript, and cannot modify this behaviour. Not without some heavy Javascript tricks.

But your difference does not represent a date, but a difference of dates. Treat is as such, and compute the hours, minutes and seconds like this:

var hours = Math.floor(difference / 36e5),
    minutes = Math.floor(difference % 36e5 / 60000),
    seconds = Math.floor(difference % 60000 / 1000);

Alternatively, you can take your timezone into account when creating lapse:

var lapse = new Date(difference + new Date().getTimezoneOffset() * 1000);

but I wouldn't recommend this: Date objects are overkill for your purposes.

MaxArt
  • 22,200
  • 10
  • 82
  • 81
  • What im tryin to represent it is a difference of dates, i will use your line 'var hours = Math.floor(difference / 36e5)' since i dont have problems with the minutes and the seconds... That should work, right? Thanks a lot! – pistoleta Apr 24 '13 at 10:35
  • i used Math.floor(difference / 36e5), and it works , just for curiosity what does it mean 36e5 ? – pistoleta Apr 24 '13 at 10:54
  • 3
    36e5 is the number 3600000 - it's called "scientific notation" and equals 36*10^5 – DThought Apr 24 '13 at 12:17
  • @DThought Exactly, it's the number of milliseconds in a hour. I use the scientific notation because it's shorter in this case. I use 6e4 too. – MaxArt Apr 24 '13 at 13:41
  • @user2314770 You're welcome. Remember to accept the answer if it helped you. And don't forget that your code may *still* have problems with the minutes if the timezone is shifted for fractions of an hour (as in India, for example). I still recommend to compute the minutes and the seconds on your own. – MaxArt Apr 24 '13 at 14:08
2

Note that in the getTimezoneOffset() returns a value in minutes, so if you want to use the lapse, you can correct it for the timezone difference like this:

lapse = new Date(difference); 
tz_correction_minutes = new Date().getTimezoneOffset() - lapse.getTimezoneOffset();
lapse.setMinutes(offset_date.getMinutes() + tz_correction_minutes);

now you can do:

label.text(lapse.getDate()-1+' days and'  +lapse.getHours()+':'+lapse.getMinutes()+':'+lapse.getSeconds());

to print out the time difference in human readable form

Matthijs
  • 1,484
  • 1
  • 14
  • 13