27

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);

4
  • 1
    This is less invasive than the other solution I had, and it works well, thanks!
    – Jeremy B.
    Jul 14, 2011 at 18:00
  • I agree :) I posted mine there too
    – mplungjan
    Jul 14, 2011 at 18:02
  • How can i convert my this 2019-11-18T06:34:01.548Z date format to simple date i.e 2019-11-18. ?
    – TMA
    Nov 20, 2019 at 8:58
  • 5
    yourDate.split("T")[0]
    – mplungjan
    Nov 20, 2019 at 9:02
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
  • You need to add 1 to getMonth to get the human readable date
    – mplungjan
    Apr 6, 2023 at 6:00
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
  • 8
    According to the docs, new Date(Date.parse(dateString)) is the same as new Date(dateString);
    – Harmen
    Jul 14, 2011 at 15:21
  • 2
    @Harmen: You seem to be correct. I was having some problems with doing it directly, but I can't remember what exactly.
    – pimvdb
    Jul 14, 2011 at 15:22
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 :)

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.

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
  • At least replace '/' with '-' which is asked in the question.
    – 0xc0de
    Aug 26, 2013 at 13:52
-1

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

2
  • 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.
    – Jeremy B.
    Jul 14, 2011 at 15:25
  • Dont work in firefox 28
    – e-info128
    Apr 9, 2014 at 20: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
  • Since this is IDENTICAL to this one posted 2 minutes before, it has the same error of the month being off by one
    – mplungjan
    Apr 6, 2023 at 6:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.