32

I have a date string "Sunday, February 28, 2010" that I would like to convert to a js date object formatted @ MM/DD/YYYY but don't know how. Any suggestions?

1
  • 1
    Maybe split() is faster, and you can write shorter code :) Commented Jul 30, 2013 at 10:50

6 Answers 6

98

If you're running with jQuery you can use the datepicker UI library's parseDate function to convert your string to a date:

var d = $.datepicker.parseDate("DD, MM dd, yy",  "Sunday, February 28, 2010");

and then follow it up with the formatDate method to get it to the string format you want

var datestrInNewFormat = $.datepicker.formatDate( "mm/dd/yy", d);

If you're not running with jQuery of course its probably not the best plan given you'd need jQuery core as well as the datepicker UI module... best to go with the suggestion from Segfault above to use date.js.

HTH

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

4 Comments

+1 for an answer using jquery. Closest answer to that question. If you are using jquery, no need to add another lib.
No that's not right. datepicker is not part of jQuery. Its part of jQuery UI. If you only have the core library then you are essentially adding another lib. If you don't have or don't plan to use jQuery UI then I would take a look at Moment.js.
@Colin, that's what I interpreted "you'd need jQuery core as well as the datepicker UI module" to mean.
@isherwood I disagree. I think that's a pretty vague sentence. I read that as needing jQuery core and some sub-module component of it. Its not clear that you also need to include jQuery UI. In fact there are lots of JS libraries out there that have "date picker UI modules." So assuming you did infer that you are to include another library its still unclear if jQuery UI is the one to get.
22

I would grab date.js or else you will need to roll your own formatting function.

1 Comment

It should be noted that date.js breaks jQuery UI datepicker as it alters the Date prototype. jQuery core says they won't fix bugs related to this.
4
var stringDate = "Sunday, February 28, 2010";

var months = ["January", "February", "March"]; // You add the rest  :-)

var m = /(\w+) (\d+), (\d+)/.exec(stringDate);

var date = new Date(+m[3], months.indexOf(m[1]), +m[2]);

The indexOf method on arrays is only supported on newer browsers (i.e. not IE). You'll need to do the searching yourself or use one of the many libraries that provide the same functionality.

Also the code is lacking any error checking which should be added. (String not matching the regular expression, non existent months, etc.)

Comments

3

If you only need it once, it's overkill to load a plugin.

For a date "dd/mm/yyyy", this works for me:

new Date(d.date.substring(6, 10),d.date.substring(3, 5)-1,d.date.substring(0, 2));

Just invert month and day for mm/dd/yyyy, the syntax is new Date(y,m,d)

Comments

3

I used the javascript date funtion toLocaleDateString to get

var Today = new Date();
var r = Today.toLocaleDateString();

The result of r will be

11/29/2016

More info at: http://www.w3schools.com/jsref/jsref_tolocaledatestring.asp

Comments

1

Use moment js for any date operation.

https://momentjs.com/

console.log(moment("Sunday, February 28, 2010").format('MM/DD/YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

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.