0

How can I create a Date object from a date with this format:

03/23/2016 02:00:00 PM

2
  • This question has been asked many, many times. Manually parse strings, either write a function or use a library. DO NOT use the Date constructor or Date.parse (they are equivalent for parsing strings). If you wan a simple parser and formatter, consider date-format.js. Commented Mar 24, 2016 at 0:42
  • Thanks @RobG! Sorry I didn't know what to search honestly.. tried too specific I guess and didn't find what I needed. Commented Mar 25, 2016 at 2:57

4 Answers 4

2

The Date object can parse strings: new Date('03/23/2016 02:00:00 PM')

For example:

var date = new Date('03/23/2016 02:00:00 PM') // => "Wed Mar 23 2016 14:00:00 GMT-0700 (PDT)"
date.getFullYear() // => 2016

However, I would recommend using a library that someone else has already spent time considering the edge cases, like time zones, etc. (a good one I've used is moment.js).

Keep in mind (from the moment.js docs):

Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

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

1 Comment

The moment.js documentation is not an authority on ECMAScript, please use ECMA-262 for that. In this case, the moment.js documentation is wrong, support for a limited selection of ISO 8601 formats was introduced in ES5, however support is still inconsistent and not to be relied upon. Also, even current browsers do not correctly parse ISO formats, so always specify the format or use a bespoke parser.
1
var date = new Date("3/23/2016 02:00:00 PM");
console.log(date);

You can then access all the methods of the Date object.

2 Comments

No, do not do that. Parsing of strings with the Date constructor (and Date.parse, it is equivalent) is unreliable. Just don't do it.
@RobG - Thank you, you make an excellent point. Where the code will be used is an important consideration. If it is a small project for learning, the most expeditous solution may well be the best. If it is targeted for any professional solution, it should be implemented with strict adherence to standards and best practices.
1

Looking at MDN var date = new Date('03/23/2016 02:00:00 PM')

1 Comment

MDN is not an authority on anything, it's a (very helpful) public wiki that anyone can contribute to. There is no requirement in the language specification for that format string to be parsed at all.
1

You can use something like date.js: First use script, then write date:

<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script> 
....
document.write(new Date().toString("dd:MM:yyyy hh:mm:ss tt"));

1 Comment

Parsing must include the format too, otherwise the results are unreliable.

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.