admin管理员组文章数量:1356304
I currently have all my dates being returned with the letter T in my JSON Strings. EX: 2019-02-03T06:48:07. Is there a way to change the JSON String to return as 02-03-2019 06:48:07?
I am also using javascript to load the dates currently into a data table.
I currently have all my dates being returned with the letter T in my JSON Strings. EX: 2019-02-03T06:48:07. Is there a way to change the JSON String to return as 02-03-2019 06:48:07?
I am also using javascript to load the dates currently into a data table.
Share Improve this question asked Jan 22, 2020 at 15:31 Steve WolfeSteve Wolfe 711 gold badge2 silver badges10 bronze badges 4- You could just remove the T. What have you tried? – GetSet Commented Jan 22, 2020 at 15:34
- Your question is a little vague. I assume this JSON data es from some sort of API. Are you maintaining this API? If so, change the data to return the date/ time in whichever format you choose. – nopassport1 Commented Jan 22, 2020 at 15:35
- Yes I manage the API. Ok I will look into changing that. – Steve Wolfe Commented Jan 22, 2020 at 15:36
- You may want to consider NOT changing it at the API. It depends on how reusable you want the API to be, but generally the ISO 8601 format is the preferred format for date/time information in an API. This allows the client code to determine the formatting. Should you want to change the format down the road, it is generally easier to alter and deploy the client code than changing the API. – musicfuel Commented Jan 22, 2020 at 15:55
2 Answers
Reset to default 2You can convert your string into a Date
object and then format it like this:
let jsonString = "2019-02-03T06:48:07";
// You should set your own timezone and options.
console.log(new Date(jsonString).toLocaleString('en-US', {hour12: false}))
For more information, visit: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
The T is there because this is a standard ISO 8601 format. It makes it very easy to parse on the client side:
var myDate = new Date('2019-02-03T06:48:07')
With myDate you can then do whatever formatting you wish. Assuming you have another function to handle leading zeros, that could be something like:
var myFormattedDate = padLeft(myDate.getMonth() + 1, '0') + '-' +
padLeft(myDate.getDay(), '0') + '-'
myDate.getFullYear() + ' ' +
// etc
Note that your timestamp lacks any timezone information. With this code it will be interpreted on the client side in whatever the local timezone of the user is. If it is UTC time, then you can correct for this by adding either 'Z' or '+00:00' onto the timestamp before parsing in the Date constructor.
If you are able to add a library to assist, this all bees much easier with moment.js:
myFormattedDate = moment('2019-02-03T06:48:07').format('MM/DD/YYYY HH:mm:ss');
本文标签: javascriptFormat JSON DateTimeStack Overflow
版权声明:本文标题:javascript - Format JSON DateTime - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744055147a2583119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论