139

How do I convert 07/26/2010 to a UNIX timestamp using Javascript?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221

7 Answers7

248

You can create a Date object, and call getTime on it:

new Date(2010, 6, 26).getTime() / 1000
Michael Allan Jackson
  • 4,217
  • 3
  • 35
  • 45
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • 15
    The month argument for the date constructor lacks a little consistency and is actually zero-based. This means 7 is August, so you need to subtract 1 :-) – Andy E Jul 29 '10 at 22:25
  • 7
    @Andy - it is a whacky idea to offset months by -1 while keeping the rest, and is exactly the kind of thing that can make a space shuttle go nuts, if JavaScript were ever to be used there :) – Anurag Jul 29 '10 at 22:27
  • 10
    Probably better to go: new Date("2010-7-26").getTime() / 1000 so you don't have to think about offsets. – Vincent McNabb Jul 29 '10 at 22:37
  • 8
    @Vincent: `new Date("2010-7-26")` will not parse in Internet Explorer. *Date* constructor relies on the *Date.parse()* method when a string is passed, which is implementation dependant in ECMA-262 3rd edition and, as a result, notoriously inflexible in IE. – Andy E Jul 30 '10 at 07:50
  • One could minimize possible errors or misunderstandings specifying the time zone too. At least the actual (Linux-) versions of Firefox, Opera and Chrome support to instantiate a JavaScript Date like new Date("Jan 1, 1970 GMT+00:00, 00:01"); which also reduces confusion about the correct month format. – Michael Besteck Aug 31 '15 at 16:50
62
new Date("2016-3-17").valueOf() 

will return a long epoch

Jyo Banerjee
  • 690
  • 6
  • 9
10

Some answers does not explain the side effects of variations in the timezone for JavaScript Date object. So you should consider this answer if this is a concern for you.

Method 1: Machine's timezone dependent

By default, JavaScript returns a Date considering the machine's timezone, so getTime() result varies from computer to computer. You can check this behavior running:

new Date(1970, 0, 1, 0, 0, 0, 0).getTime()
    // Since 1970-01-01 is Epoch, you may expect ZERO
    // but in fact the result varies based on computer's timezone

This is not a problem if you really want the time since Epoch considering your timezone. So if you want to get time since Epoch for the current Date or even a specified Date based on the computer's timezone, you're free to continue using this method.

// Seconds since Epoch (Unix timestamp format)

new Date().getTime() / 1000             // local Date/Time since Epoch in seconds
new Date(2020, 11, 1).getTime() / 1000  // time since Epoch to 2020-12-01 00:00 (local timezone) in seconds

// Milliseconds since Epoch (used by some systems, eg. JavaScript itself)

new Date().getTime()                    // local Date/Time since Epoch in milliseconds
new Date(2020,  0, 2).getTime()         // time since Epoch to 2020-01-02 00:00 (local timezone) in milliseconds

// **Warning**: notice that MONTHS in JavaScript Dates starts in zero (0 = January, 11 = December)

Method 2: Machine's timezone independent

However, if you want to get ride of variations in timezone and get time since Epoch for a specified Date in UTC (that is, timezone independent), you need to use Date.UTC method or shift the date from your timezone to UTC:

Date.UTC(1970,  0, 1)
    // should be ZERO in any computer, since it is ZERO the difference from Epoch

    // Alternatively (if, for some reason, you do not want Date.UTC)
    const timezone_diff = new Date(1970, 0, 1).getTime()  // difference in milliseconds between your timezone and UTC
    (new Date(1970,  0, 1).getTime() - timezone_diff)
    // should be ZERO in any computer, since it is ZERO the difference from Epoch

So, using this method (or, alternatively, subtracting the difference), the result should be:

// Seconds since Epoch (Unix timestamp format)

Date.UTC(2020,  0, 1) / 1000  // time since Epoch to 2020-01-01 00:00 UTC in seconds

    // Alternatively (if, for some reason, you do not want Date.UTC)
    const timezone_diff = new Date(1970, 0, 1).getTime()
    (new Date(2020,  0, 1).getTime() - timezone_diff) / 1000  // time since Epoch to 2020-01-01 00:00 UTC in seconds
    (new Date(2020, 11, 1).getTime() - timezone_diff) / 1000  // time since Epoch to 2020-12-01 00:00 UTC in seconds

// Milliseconds since Epoch (used by some systems, eg. JavaScript itself)

Date.UTC(2020,  0, 2)   // time since Epoch to 2020-01-02 00:00 UTC in milliseconds

    // Alternatively (if, for some reason, you do not want Date.UTC)
    const timezone_diff = new Date(1970, 0, 1).getTime()
    (new Date(2020,  0, 2).getTime() - timezone_diff)         // time since Epoch to 2020-01-02 00:00 UTC in milliseconds

// **Warning**: notice that MONTHS in JavaScript Dates starts in zero (0 = January, 11 = December)

IMO, unless you know what you're doing (see note above), you should prefer Method 2, since it is machine independent.


End note

Although the recomendations in this answer, and since Date.UTC does not work without a specified date/time, you may be inclined in using the alternative approach and doing something like this:

const timezone_diff = new Date(1970, 0, 1).getTime()
(new Date().getTime() - timezone_diff)  // <-- !!! new Date() without arguments
    // means "local Date/Time subtracted by timezone since Epoch" (?)

This does not make any sense and it is probably WRONG (you are modifying the date). Be aware of not doing this. If you want to get time since Epoch from the current date AND TIME, you are most probably OK using Method 1.

Diego Queiroz
  • 3,198
  • 1
  • 24
  • 36
5

Take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp

There is a function UTC() that returns the milliseconds from the unix epoch.

Mitch Dempsey
  • 38,725
  • 6
  • 68
  • 74
4

Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
const javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT');

console.log(unixTimeZero);
// expected output: 0

console.log(javaScriptRelease);
// expected output: 818035920000

Explore more at: Date.parse()

Sourabh
  • 1,515
  • 1
  • 14
  • 21
3

You can also use Date.now() function.

Neha M
  • 443
  • 1
  • 4
  • 13
  • 1
    Technically did not answer the OP question of converting a _particular_ date to seconds-since-the-epoch. But I like the concise-ness. Clearly `Date.now()` is better than `new Date().getTime()` in most cases. – MarkHu Feb 19 '21 at 22:33
0
Number(new Date(2010, 6, 26))

Works the same way as things above. If you need seconds don't forget to / 1000

Andrii Los
  • 486
  • 5
  • 8