Reputation: 1457
Am trying to convert a string to date
var strdate='2014-04-23+09:06:57.4830591330'
while trying to convert this string to date using below code
var followupDate = new Date(strdate);
console.log(followupDate)
am getting the error
Date {Invalid Date}
Upvotes: 0
Views: 352
Reputation: 36786
You just need a space rather than an addition sign, so you could just replace it:
var strdate='2014-04-23+09:06:57.4830591330';
var followupDate = new Date(strdate.replace("+"," "));
console.log(followupDate);
Will log something like: Wed Apr 23 2014 09:06:57 GMT+0100 (GMT Summer Time)
.
Upvotes: 3
Reputation: 1695
Your String Format is Wrong It should be like this
var strdate = "2014-04-23 09:06:57.4830591330"
if you are getting your value dynamicaly then change it to this format by
var newstrdate = strdate.replace("+", " ");
and then try
date = new Date(newstrdate);
Upvotes: 0
Reputation: 10700
You should replace the + sign in your string for a space, between the year and the hours.
To explain this, let's see Date documentation :
dateString
String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).
Now, from RFC 2822 (under 3.3. Date and Time Specification), it's explicitly described that the + is meant for timezones:
zone = (( "+" / "-" ) 4DIGIT) / obs-zone
Moreover, note that there's no room for milliseconds in this standard.
Upvotes: 0