2

In Java we can do it like this:

new Date(10L);

That's 10 milliseconds since 1 January 1970 UTC.

Per the MDN docs it does not look like Javascript has a similar constructor. Do we need to do something like:

new Date(0,0,0,0,0,0,10);
Ole
  • 41,793
  • 59
  • 191
  • 359

1 Answers1

4

Not that hard:

new Date(10);

The C-style L postfix is not a JavaScript feature, but otherwise Date() works as expected.

One of the constructor methods is:

new Date(value);

Where value is specifically defined as:

Integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC, with leap seconds ignored (Unix Epoch; but consider that most Unix timestamp functions count in seconds).

This is exactly what you're looking for, so it works just like you'd expect from Java.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    I just tried it in a JavaScript console and it worked, so hey. – tadman Oct 31 '18 at 23:28
  • 1
    @Ole If you're looking at MDN it's the `new Date(value)` example. – lonesomeday Oct 31 '18 at 23:28
  • @lonesomeday if only they had said `milliseconds` instead of `value`, allthough I agree that deductively it's kind of obvious.... Anyways we have the full context now. Happy Halloween guys! – Ole Oct 31 '18 at 23:35
  • 1
    To be fair, it's a *value* expressed in milliseconds. MDN isn't wrong! You can also give it other types of values. – tadman Oct 31 '18 at 23:37
  • 1
    @Ole It does describe it as being milliseconds in four different places on that page... – lonesomeday Nov 01 '18 at 08:39
  • @lonesomeday that's true, but none of the contexts in the four different places are related to the constructor. For example it says ... Date objects are based on a time value that is the number of milliseconds since 1 January 1970 UTC .... But this is not related to what the question is asking. – Ole Nov 01 '18 at 14:11
  • 1
    The `value` argument is given a very specific definition if you look more closely at the documentation. – tadman Nov 01 '18 at 17:27