1
this.D = new Date(1433760825 * 1000);   
this.NewD = this.D.getHours();

D = "2015-06-08T10:53:45.000Z" - this is fine, it is what I was expecting to get. But...but....NewD results to 11 and Not 10. Why???

Thanks!

3 Answers3

0

When you instantiate the Date object using a value like this, you get a date based on UTC. From MDN:

Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

When you subsequently call the getHours() method, you get the hours in your local time zone.


So for your example:

var sampleDate = new Date(1433760825 * 1000);   
var hours = sampleDate.getUTCHours();
alert(sampleDate);
alert(this.hours);

Should get you the result you are looking for.

Paddy
  • 33,309
  • 15
  • 79
  • 114
  • getUTCHours works, it returns 10. However my local time is not +1 and I do not undertand why getHours returns 11 instead of 10. – user4854731 Jun 08 '15 at 14:42
  • Unfortunately I really have no idea where you are, so can't help you there :) Is it possibly something to do with British Summer Time (BST)? – Paddy Jun 08 '15 at 14:49
  • You may want to look here: http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript – Paddy Jun 08 '15 at 14:50
  • location is in London – user4854731 Jun 09 '15 at 10:27
  • Then your issue is British Summer Time. During the summer months we are UTC + 1. http://www.timeanddate.com/time/zones/bst – Paddy Jun 09 '15 at 10:43
-1

A couple of quick points:

  1. Don't use single character variable names, and don't capitalize them if you do

  2. D is not equal to the date that you put in.

  3. The new Date(value) is expecting an integer and you are giving it something larger then an integer. So it is defaulting back to current time.

Try using a DateString, or other method as described in this documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Jeremy
  • 51
  • 1
  • 4
  • This is not larger than the constructor will expect. Max integer values: http://ecma262-5.com/ELS5_HTML.htm#Section_8.5. – Paddy Jun 08 '15 at 14:24
-1

When you create a DateObject, if you try to print it, it will print the datetime according to UTC. But when you do the getHours() property it will tell you the hours passed according to your own local time zone.

(new DateTime()).getHours()

This will return the hours in (UTC + offset) according to your timezone.