Date formatting with / without Moment.js

I am trying to reformat the date I get from the API. In the object I have:

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

I would like to show the date as 6/13/2013. Someone suggested using moment.js. This has tons of documentation, but I'm a bit confused about how to use it. can anyone help or suggest an easier way to do this?

+6
source share
4 answers

No need to change the original string, you can just use it as follows:

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

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

+8
source

In moments you can just do it

 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, not 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 

Using a Date object, see @ Antony . Answer deleted

Or, if you need 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 its constituent parts and pass it to Date.UTC using these arguments, instead of Date parsing the string.

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

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

+4
source

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

You can check this API of time formats - https://www.mashape.com/parsify/format#!endpoint-Time

I printed your date "2013-06-13T16: 29: 55.245Z" and received 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
source

Source: https://habr.com/ru/post/947234/


All Articles