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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With