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?

sadmicrowave
  • 39,964
  • 34
  • 108
  • 180

6 Answers6

99

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

Jono Wilkinson
  • 991
  • 1
  • 6
  • 2
  • 8
    +1 for an answer using jquery. Closest answer to that question. If you are using jquery, no need to add another lib. – Guilherme David da Costa Feb 08 '12 at 18:25
  • 3
    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 Pear Jul 07 '14 at 02:10
  • 2
    @Colin, that's what I interpreted "you'd need jQuery core as well as the datepicker UI module" to mean. – isherwood Aug 27 '14 at 15:41
  • 1
    @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. – Colin Pear Aug 27 '14 at 17:55
22

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

Segfault
  • 8,036
  • 3
  • 35
  • 54
  • 8
    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. – SupaIrish Sep 02 '12 at 22:07
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.)

RoToRa
  • 37,635
  • 12
  • 69
  • 105
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)

stallingOne
  • 3,633
  • 3
  • 41
  • 63
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

MUlferts
  • 1,310
  • 2
  • 16
  • 30
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>
Vijay Maheriya
  • 1,617
  • 16
  • 21