28

I have a datetime string being provided to me in the following format:

yyyy-MM-dd HH:mm:ss
2011-07-14 11:23:00

When attempting to parse it into a JavaScript date() object it fails. What is the best way to convert this into a format that JavaScript can understand?

The answers below suggest something like

var myDate = new Date('2011-07-14 11:23:00');

Which is what I was using. It appears this may be a browser issue. I've made a http://jsfiddle.net/czeBu/ for this. It works OK for me in Chrome. In Firefox 5.0.1 on OS X it returns Invalid Date.

0

8 Answers 8

49

The failure is up to the browser implementation's handling of Date.parse and how they allow for date strings not conform the date standards

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 calendar date extended format.
The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

So we can force your string into this

const getDateFromString = str => {
  const [date,time] = str.split(" ");
  // reformat string into YYYY-MM-DDTHH:mm:ss.sssZ
  str = `${date}T${time}.000Z`
  return new Date(str);
};
let date = getDateFromString('2011-07-14 11:23:00');
console.log(date)


Older answer:

As for your format, some OSX browsers insist on the dashes being slashes.

This works everywhere including Safari 5 and Firefox 5 on OS X.

UPDATE: Fx Quantum (54) has no need for the replace, but Safari 11 is still not happy unless you convert as below

var date_test = new Date("2011-07-14 11:23:00".replace(/-/g,"/"));
console.log(date_test);

Sign up to request clarification or add additional context in comments.

4 Comments

This is less invasive than the other solution I had, and it works well, thanks!
I agree :) I posted mine there too
How can i convert my this 2019-11-18T06:34:01.548Z date format to simple date i.e 2019-11-18. ?
yourDate.split("T")[0]
5

One can use the getmonth and getday methods to get only the date.

Here I attach my solution:

var fullDate = new Date(); console.log(fullDate);
var twoDigitMonth = fullDate.getMonth() + "";
if (twoDigitMonth.length == 1)
    twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1)
    twoDigitDate = "0" + twoDigitDate;
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear(); console.log(currentDate);

1 Comment

You need to add 1 to getMonth to get the human readable date
3

Just use Date.parse() which returns a Number, then use new Date() to parse it:

var thedate = new Date(Date.parse("2011-07-14 11:23:00"));

2 Comments

According to the docs, new Date(Date.parse(dateString)) is the same as new Date(dateString);
@Harmen: You seem to be correct. I was having some problems with doing it directly, but I can't remember what exactly.
3

You can use moment.js for that, it will convert DateTime object into valid Javascript formated date:

   moment(DateOfBirth).format('DD-MMM-YYYY'); // put format as you want 

   Output: 28-Apr-1993

Hope it will help you :)

Comments

2

Use:

enter code var moment = require('moment')
var startDate = moment('2013-5-11 8:73:18', 'YYYY-M-DD HH:mm:ss')

Moment.js works very well. You can read more about it here.

Comments

0
function ConvertDateFromDiv(divTimeStr) {
    //eg:-divTimeStr=18/03/2013 12:53:00
    var tmstr = divTimeStr.toString().split(' '); //'21-01-2013 PM 3:20:24'
    var dt = tmstr[0].split('/');
    var str = dt[2] + "/" + dt[1] + "/" + dt[0] + " " + tmstr[1]; //+ " " + tmstr[1]//'2013/01/20 3:20:24 pm'
    var time = new Date(str);
    if (time == "Invalid Date") {
        time = new Date(divTimeStr);
    }
    return time;
}

1 Comment

At least replace '/' with '-' which is asked in the question.
-1

new Date("2011-07-14 11:23:00"); works fine for me.

2 Comments

Yes and no. It works for me in Chrome, but run this in FF 5.0.1 jsfiddle.net/czeBu and it will return invalid date.
Dont work in firefox 28
-1

You can use get methods:

var fullDate = new Date();
console.log(fullDate);
var twoDigitMonth = fullDate.getMonth() + "";
if (twoDigitMonth.length == 1)
    twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1)
    twoDigitDate = "0" + twoDigitDate;
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear(); console.log(currentDate);

1 Comment

Since this is IDENTICAL to this one posted 2 minutes before, it has the same error of the month being off by one

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.