date formatting with/without Moment.js

2019-01-19 17:05发布

问题:

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 to display the date as 6/13/2013. Someone suggested I use moment.js. It has tons of documentation but i'm a bit confused on how to use it. can someone please help or suggest an easier way to do this?

回答1:

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/



回答2:

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.



回答3:

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("/");


回答4:

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
}