0

I have a string which contains DateTime as "20140121230000" . If i try to convert this into a Date.

var oDate = new Date(20140121230000);

i'm getting the year as 2068! Is there a way to convert this into a Date which is of year 2014, month 01 Date 21 Time 23:00:00 ?

Is it possible to directly convert this without doing any parsing in the string ?

ikiw
  • 372
  • 1
  • 4
  • 17

4 Answers4

1

The constructor you used takes millisecond since 1st Jan, 1970, try using :

 var oDate = new Date(2014, 01, 21, 23, 00, 00, 00);

Note, month 01 will be Feb, not Jan.

Aditya Jain
  • 1,077
  • 1
  • 12
  • 25
1

Unless you use a library there is no way to convert the value without manually splitting the string.

var year   = +oDate.slice( 0, 4);
var month  = +oDate.slice( 4, 2) - 1; // Month is zero-based.
var day    = +oDate.slice( 6, 2); 

var hour   = +oDate.slice( 8, 2); 
var minute = +oDate.slice(10, 2); 
var second = +oDate.slice(12, 2);

// This will interpret the date as local time date.
var date = new Date(year, month, day, hour, minute, second);

// This will interpret the date as UTC date.
var utcDate = Date.UTC(year, month, day, hour, minute, second);
Community
  • 1
  • 1
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
  • It works like a charm. But when i do date.toJSON() it outputs ""2013-12-30T18:30:00.000Z". Here the hour & minutes seems to be wrong. Hour has to be 23 and minutes have to be 00. ANy ideas ? – ikiw May 14 '14 at 10:08
  • 1
    What timezone are you in? The answer you've selected treats the values as local, JSON is returning UTC. The value is also shifted about 23 days earlier. – RobG May 14 '14 at 10:09
  • IST(Indian Standard Time) – ikiw May 14 '14 at 10:10
  • So if you have a local time of 2014-01-01T00:00:00+0530, then the UTC equivalent (as used by JSON) is 2013-12-31T18:30:00Z. – RobG May 14 '14 at 10:12
  • Use `Date.UTC` if the date is not in local time - updated the answer. – Daniel Brückner May 14 '14 at 10:19
0

Constructing a Date object with a string

new Date(string)

expects a date string that Date.parse can understand:

  • ISO 8601 (e.g. 2011-10-10T14:48:00), or
  • RFC2822 (e.g., Mon, 25 Dec 1995 13:30:00 GMT)

See MDN for more information on Date and Date.parse.

Yours is not a recognized format. You can either

  1. reformat the string to fit one of the formats above
  2. split the string manually and call Date with individual parameters, as in new Date(year, month, day, hour, minute, second, millisecond)
  3. use a library like moment.js

Using moment.js would look something like this:

moment("20140121230000", "YYYYDDMMHHmmss")

See moment.js string + format for more information about the format syntax.

janfoeh
  • 10,243
  • 2
  • 31
  • 56
  • Please don't suggest using *Date.parse* at all. It's unreliable due to browser differences and if the string must be parsed and reformatted to suit some implementation's parser, then it may as well be given to the the Date constructor, which is reliable everywhere. Your example of ISO 8601 is treated differently by, for example, Chrome and Firefox and will result in NaN if parsed in IE 8. – RobG May 14 '14 at 10:16
  • It was not really my intention to advocate the use of `Date.parse`. I was rather trying to explain why the posters approach failed, and what formats would be acceptable when invoking `Date` with a string. Thanks for the heads up on the parsing differences regarding ISO 8601! For everyone else interested, this is a good overview: http://stackoverflow.com/a/9363445/100342 – janfoeh May 15 '14 at 00:03
0

Given '20140121230000', you can split it into bits and give it to the Date constructor:

function parseSToDate(s) {
  var b = s.match(/\d\d/g) || [];
  return new Date( (b[0] + b[1]), --b[2], b[3], b[4], b[5], b[6]);
}

console.log(parseSToDate('20140121230000'));
RobG
  • 142,382
  • 31
  • 172
  • 209