date formatting with/without Moment.js

后端 未结 4 637
旧巷少年郎
旧巷少年郎 2020-12-11 04:17

I am trying to reformat a date that I am getting from an API. In the object I have:

created_at: \"2013-06-13T16:29:55.245Z\"

I would like

相关标签:
4条回答
  • 2020-12-11 04:51

    maybe you can use split

    var tuple = createdAt.split("T");
    var date = tuple[0];
    var dateTuple = date.split("-");
    var day = parseInt(dateTuple[2]);
    var month = parseInt(dateTuple[1]);
    var year = parseInt(dateTuple[0]);
    var newFormatedDate = [ month , day,  year ].join("/");
    
    0 讨论(0)
  • 2020-12-11 04:57

    No need to modify the original string, you can just use it like this:

    alert(moment("2013-06-13T16:29:55.245Z").format("M/DD/YYYY"));
    

    Works well: http://jsfiddle.net/K5ub8/2/

    0 讨论(0)
  • 2020-12-11 05:01

    You can check out this Format Time API - https://www.mashape.com/parsify/format#!endpoint-Time

    I typed in your date "2013-06-13T16:29:55.245Z" and got the following response -

    {
      "given": "2013-06-13T16:29:55.245Z",
      "time": {
      "daysInMonth": 30,
      "millisecond": 245,
      "second": 55,
      "minute": 29,
      "hour": 16,
      "date": 13,
      "day": 4,
      "week": 24,
      "month": 5,
      "year": 2013,
      "zone": "+0000"
     },
      "formatted": {
      "weekday": "Thursday",
      "month": "June",
      "ago": "2 hours",
      "calendar": "Today at 4:29 PM",
      "generic": "2013-06-13T16:29:55+00:00",
      "time": "4:29 PM",
      "short": "06/13/2013",
      "slim": "6/13/2013",
      "hand": "Jun 13 2013",
      "handTime": "Jun 13 2013 4:29 PM",
      "longhand": "June 13 2013",
      "longhandTime": "June 13 2013 4:29 PM",
      "full": "Thursday, June 13 2013 4:29 PM",
      "fullSlim": "Thu, Jun 13 2013 4:29 PM"
     },
      "array": [
       2013,
       5,
       13,
       16,
       29,
       55,
       245
      ],
     "offset": 1371140995245,
     "unix": 1371140995,
     "utc": "2013-06-13T16:29:55.245Z",
     "valid": true,
     "integer": false,
     "zone": 0
    }
    
    0 讨论(0)
  • 2020-12-11 05:12

    In moments you can just do this

    var timeStr = "2013-06-13T16:29:55.245Z",
        newFormat = moment(timeStr).format('M/DD/YYYY');
    
    document.body.textContent = newFormat;
    <script src="https://rawgithub.com/timrwood/moment/2.9.0/min/moment.min.js"></script>

    Output

    6/13/2013 
    

    Without moments and using pure string manipulation rather than a new Date object, you could do

    var timeStr = "2013-06-13T16:29:55.245Z",
        temp = timeStr.split("T")[0].split("-").reverse(),
        newFormat;
    
    temp[0] = temp.splice(1, 1, temp[0])[0];
    newFormat = temp.join("/");
    if (newFormat.charAt(0) === "0") {
      newFormat = newFormat.slice(1);
    }
    
    document.body.textContent = newFormat;

    Output

    6/13/2013 
    

    By using the Date object see @Antony answer. Answer removed

    Or if you need it to be more cross-browser compatible with the Date object but still string parsing.

    var timeStr = "2013-06-13T16:29:55.245Z",
        intermediate = timeStr.split("T"),
        newStr = intermediate[0].split("-").join("/") + " " + intermediate[1].split(".")[0] + " GMT",
        newDate = new Date(newStr),
        newFormat = (1 + newDate.getUTCMonth()) + "/" + newDate.getUTCDate() + "/" + newDate.getFullYear();
    
    document.body.textContent = newFormat;

    Output

    6/13/2013 
    

    Finally, you can split the string into component parts and feed it into Date.UTC using these arguments, rather than let Date do the string parsing.

    Date.UTC(year, month, day [, hour, minute, second, millisecond]);

    So perhaps you can now see why people suggest using moments.js, but so long as you have the knowledge then it is not too painful to do it yourself without a library.

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