admin管理员组文章数量:1399329
I have a time format returning from MySQL at YYYY-MM-DDTHH:mm:ss.sssZ
So it looks like this
2017-02-17T22:32:25.000Z
When I run split() on it, it does nothing so
var test = data[i].createdAt;
test.split("T");
It does nothing to it
So I want the data to look like this
2017-02-17T22:32:25.000Z
to
02-17-2017 10:32 p.m.
Any help?
I have a time format returning from MySQL at YYYY-MM-DDTHH:mm:ss.sssZ
So it looks like this
2017-02-17T22:32:25.000Z
When I run split() on it, it does nothing so
var test = data[i].createdAt;
test.split("T");
It does nothing to it
So I want the data to look like this
2017-02-17T22:32:25.000Z
to
02-17-2017 10:32 p.m.
Any help?
Share Improve this question asked Feb 17, 2017 at 22:54 heybitheybit 2153 gold badges5 silver badges10 bronze badges 2-
4
split doesn't modify its argument; instead it returns the array. Also be aware that
Z
denotes UTC time, not local time, so if you do just a formatting like you suggest, you'll still not have the time in the local time zone. – trincot Commented Feb 17, 2017 at 22:58 - If some standard function doesn't seem to be working, why not read some documentation for that function to see if you're using it correctly? – nnnnnn Commented Feb 18, 2017 at 0:42
5 Answers
Reset to default 2You may want to use new Date(dateString)
and dateObj.toLocaleTimeString([locales[, options]])
:
var date = new Date('2017-02-17T22:32:25.000Z');
var formatOptions = {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: true
};
var dateString = date.toLocaleDateString('en-US', formatOptions);
// => "02/17/2017, 11:32 PM"
dateString = dateString.replace(',', '')
.replace('PM', 'p.m.')
.replace('AM', 'a.m.');
// => "02/17/2017 11:32 p.m."
console.log(dateString);
Date.parse(dateString)
dateObj.toLocaleTimeString([locales[, options]])
If you are not averse to using a Javascript library, I would highly remend Moment: http://momentjs.. For your use case, the code would be:
var time = moment(data[i].createdAt).format('MMMM Do YYYY, h:mm a');
It handles multiple time formats (on input and output) as well as edge cases.
You could use toLocaleString
providing it the optional arguments. For instance:
var dt = '2017-02-17T22:32:25.000Z';
console.log(new Date(dt).toLocaleString('en-CA', { hour12:true }));
The JavaScript Date
object will accept ISO dates like yours, at which point you can then just manually format the string like so:
function formatDate(d) {
var myDate = new Date(d);
var hrs = ((myDate.getHours() > 12) ? myDate.getHours()-12 : myDate.getHours());
var amPM = ((myDate.getHours() >= 12) ? "PM" : "AM");
if (hrs==0) hrs = 12;
var formattedDate = (myDate.getMonth() + 1) + "-" + myDate.getDate() + "-" + myDate.getFullYear() + " " + hrs + ":" + myDate.getMinutes() + " " + amPM;
return formattedDate;
}
var myFormattedDate = formatDate("2017-02-17T22:32:25.000Z");
alert(myFormattedDate);
You can use toLocaleDateString
and toLocaleTimeString
with or without some options. In contrast with toLocaleString
this allows you to not include a ma.
const str = '2017-02-17T22:32:25.000Z';
const d = new Date(str);
console.log(`${d.toLocaleDateString()} ${d.toLocaleTimeString()}`);
console.log(`${d.toLocaleDateString().replace(/\//g,'-')} ${d.toLocaleTimeString('en-US', {hour: '2-digit', minute:'2-digit'})}`);
本文标签: javascriptHow to convert time format YYYYMMDDTHHmmsssssZ to MMDDYY and standard timeStack Overflow
版权声明:本文标题:javascript - How to convert time format YYYY-MM-DDTHH:mm:ss.sssZ to MM-DD-YY and standard time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744204610a2595126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论