Java date format to JavaScript date format

后端 未结 10 2080
余生分开走
余生分开走 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 + "<br/>New Format: " + dateNew);
    
    0 讨论(0)
  • 2020-12-03 01:15

    You could use my plugin jquery-dateFormat.

    // Text
    $.format.date("2009-12-18 10:54:50.546", "dd/MM/yyyy");
    // HTML Object
    $.format.date($("#spanDate").text(), "dd/MM/yyyy");
    // Scriptlet
    $.format.date("<%=java.util.Date().toString()%>", "dd/MM/yyyy");
    // JSON
    var obj = ajaxRequest();
    $.format.date(obj.date, "dd/MM/yyyy");
    
    0 讨论(0)
  • 2020-12-03 01:16

    Check out moment.js! It's "A lightweight javascript date library for parsing, manipulating, and formatting dates". It is a really powerful little library.

    Here's an example...

    var today = moment(new Date());
    today.format("MMMM D, YYYY h:m A");        // outputs "April 11, 2012 2:32 PM"
    
    // in one line...
    moment().format("MMMM D, YYYY h:m A");     // outputs "April 11, 2012 2:32 PM"
    

    Here's another example...

    var a = moment([2012, 2, 12, 15, 25, 50, 125]);
    a.format("ffffdd, MMMM Do YYYY, h:mm:ss a"); // "Monday, March 12th 2012, 3:25:50 pm"
    a.format("ffffd, hA");                       // "Mon, 3PM"
    a.format("D/M/YYYY");                      // "12/3/2012"
    

    Also, its worth mentioning to checkout date.js. I think the two libraries complement each other.

    0 讨论(0)
  • 2020-12-03 01:19

    The javascript code in this page implements some date functions and they "use the same format strings as the java.text.SimpleDateFormat class, with a few minor exceptions". It is not the very same as you want but it can be a good start point.

    0 讨论(0)
  • A similar topic has been answered here: Converting dates in JavaScript

    I personally have found this to be a rather large pain and took the author's suggestion and used a library. As noted, jQuery datepicker has one that is a viable solution if you can afford the overhead of download for your application or already using it.

    0 讨论(0)
  • 2020-12-03 01:30

    If you are using java, take a look at the Simple Date Format class.

    0 讨论(0)
提交回复
热议问题