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 ?

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

5 Comments

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 ?
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.
IST(Indian Standard Time)
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.
Use Date.UTC if the date is not in local time - updated the answer.
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.

Comments

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.

2 Comments

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.
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: stackoverflow.com/a/9363445/100342
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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.