jquery/javascript convert date string to date

前端 未结 6 466
抹茶落季
抹茶落季 2020-12-05 13:11

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?

6条回答
  •  被撕碎了的回忆
    2020-12-05 13:42

    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

提交回复
热议问题