Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery/javascript convert date string to date

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?

like image 276
sadmicrowave Avatar asked Jun 04 '10 13:06

sadmicrowave


People also ask

How convert YYYY MM DD string to date in JavaScript?

To convert a date string (YYYYMMDD) to a date with JavaScript, we can call the JavaScript string's substring method to extract the year, month, and day from the string. Then we can use the Date constructor to convert it to a JavaScript date.


1 Answers

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

like image 166
Jono Wilkinson Avatar answered Sep 24 '22 17:09

Jono Wilkinson