5

I am trying to understand something about getTime(), My problem is that, I am setting up a new Date with lets say: 23,07,2012. When I am using getTime() on it I should get the milliseconds Since 01,01,1970. When I divide the value I getting from getTime() with (1000*60*60*24) I should get the days number was pass since 01,01,1970 until 05,07,2012 but somehow I get a number with a Decimal point (15543.875) I dont understand why, I mean since 01,01,1970 and 23,07,2012 I should get an integer(that what I think), well I know I am really wrong, if can someone please help me understand why the decimal point at the result.

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
Aviel Fedida
  • 4,004
  • 9
  • 54
  • 88

1 Answers1

4

If you are setting a date like new Date(2012, 06, 23) It will be set according to the timezone of the client, where as .getTime() is UTC. You want Date.UTC:

Date.UTC(2012,6,23) / (1000*60*60*24)
//15544 For any computer

new Date(2012, 06, 23) / (1000*60*60*24)
//15543.875 For my computer, I am coincidentally in  the same timezone as Israel. This result will depend on what timezone the client is.
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • thank you esailija can you please upload a code showing resoult of my timezone and the UTC timezone devide with (1000*60*60*24) becouse the link is not working and i cant write the code down its not working mabye i didnt got you right so if you can please upload thank you. – Aviel Fedida Jul 19 '12 at 13:04
  • @uBlankText all the code you need is in my answer. Show me your non-working code here: http://jsfiddle.net/ – Esailija Jul 19 '12 at 13:09
  • So the decimal point is becouse my timezone is falling behind in few hours and that why i should use the ceil method or the UTC? – Aviel Fedida Jul 19 '12 at 13:34
  • @uBlankText you should only use the `UTC`. The decimal point is just a symptom, the real cause is the timezones which you can ignore when you use `UTC`. So use `UTC`. – Esailija Jul 19 '12 at 15:26