1

I'm located in PST timezone and I want to be able to take the string "2014-01-01" and convert it into Unix time without "2014-01-01" getting converted to PST.

Here's what I'm doing:

Date.parse(new Date("2014-01-01"))

I'm getting the value 1388534400000 which is equivalent to Tue Dec 31 2013 16:00:00 GMT-0800 (Pacific Standard Time)

I want to take the date as "2014-01-01" and not convert it into PST before converting it into Unix time.

codeBarer
  • 2,238
  • 7
  • 44
  • 75

2 Answers2

3

A few things:

  • The Date constructor returns a Date object, not a string. You shouldn't wrap it in a call to Date.parse.

    If you want a unix timestamp, just call getTime().

    var ts = new Date("2014-01-01").getTime();
    

    Alternatively, you can parse the date string without creating a Date object at all.

    var ts = Date.parse("2014-01-01");
    
  • The behavior of date parsing in JavaScript is implementation dependent. Most browsers will already interpret a yyyy-mm-dd string to be in UTC, due to the dashes (-). If you replace with with slashes (/), you'll see the string get interpreted as local time instead.

  • I think you're confused about the output. You said the timestamp was equivalent to PST, but that's just one representation. It's also equivalent to the UTC value you passed in. It's not getting converted in the input, it's being converted when you are converting the timestamp back to local time.

  • You can use a library like moment.js, which gives you full control of the input and output. This is usually the best option, but has the overhead of including a library in your application.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

Another way to convert the specified date string to Unix time is as follows:

var str = "2014-01-01";
var parts   = str.split('-');
parts[1]   -= 1;                                   // js numeric mos are 0-11
var ms = Date.UTC( parts[0], parts[1], parts[2] ); // parts: YYYY, MM, DD
var unix_time = ms/1000;                           // Unix time uses seconds
console.log("Unix time: " + unix_time);

Date.UTC() returns the number of milliseconds occurring since January 1, 1970 midnight up to the instant of the specified date, irrespective of any timezone. The script transforms the result into Unix time, i.e. seconds, by dividing the number of milliseconds by 1000.

After splitting the string into an array, the code adjusts the element containing the month, lest JavaScript mistake its value for March; JavaScript comprehends numeric months as ranging from 0-11, not 1-12. Next, the script passes the elements sequentially in accordance with the year, month, day parameters that Date.UTC requires. Although UTC() expects numbers for parameters, it accepts the numerical strings.

Note: if you first create a new date object and expect to use a UTC method -- that results in an error because it is a static method of JavaScript's Date Object.

You may check the validity of the UTC() return value, using the aforementioned variables ms and str, as follows:

console.log( new Date( str ).toUTCString( ms ));

The output: Wed, 01 Jan 2014 00:00:00 GMT

See live demo here)

Passing a date string to the Date constructor instead of the numerical parameters it expects affords an unexpected benefit; the date string is treated as if it's timezone is UTC, i.e. zero by the local date object. Once created, the local date object executes its toUTCString() method to attain the above-indicated result. The toString() method would also yield the same output, but it appends local timezone information.

slevy1
  • 3,797
  • 2
  • 27
  • 33