3

In Javascript, given d=new Date() it might be the case that d.getHours()!=d.getUtcHours().

I can construct a Date object, giving the components explicitly

new Date (y, m, d, hrs, mins)

but if these components are UTC then I have a problem. From the docs

The UTC() method differs from the Date constructor in two ways.

  • Date.UTC() uses universal time instead of the local time.

  • Date.UTC() returns a time value as a number instead of creating a Date object.

How do I construct a Date object given UTC components?

spraff
  • 32,570
  • 22
  • 121
  • 229

3 Answers3

5

according to this answer you can do it like this:

new Date(Date.UTC(year, month, day, hour, minute, second))

Community
  • 1
  • 1
pwolaq
  • 6,343
  • 19
  • 45
1

How about new Date(Date.UTC(...)), e.g. new Date(Date.UTC(2016, 9, 5, 13, 11, 5)).

redneb
  • 21,794
  • 6
  • 42
  • 54
0
var milliseconds = Date.UTC(y, m, d, hrs, mins);
var date = new Date(milliseconds);
Umair Farooq
  • 1,684
  • 2
  • 14
  • 25