Java date format to JavaScript date format

后端 未结 10 2108
余生分开走
余生分开走 2020-12-03 01:10

I would like to be able to convert a Java date format string, e.g. dd/MM/yyyy (07/06/2009) to a JavaScript date format string, e.g. dd/mm/yy (07/06/2009).

10条回答
  •  一整个雨季
    2020-12-03 01:14

    This JavaScript library should be able to help you.

    http://plugins.jquery.com/project/fIsForFormat

    (I don't know why they have it as a jQuery Plugin, because it works standalone.)

    You'd simply split the original formatted date into its individual elements and then create a new Date Object with those elements. Then, use this library's "Date.f()" method to output it into any format you could want.

    For example:

    var dateOld = "11/27/2010",
        dateArr = date1.split("/"),
        dateObj = new Date(dateArr[2], dateArr[0], dateArr[1]),
        dateNew = dateObj.f("MMM d, yyyy");
    
    document.write("Old Format: " + dateOld + "
    New Format: " + dateNew);

提交回复
热议问题