[英]Format datetime to DD-MMM-YYY HH:mm:ss using moment.js
I have date in this format 我有这种格式的日期
2017-01-09T18:30:00.000Z
I am using moment.js and I am trying to convert it into DD-MMM-YYY HH:mm:ss
--> 09-Jan-2017 18:30:00
我正在使用moment.js,并且试图将其转换为
DD-MMM-YYY HH:mm:ss
> 09-Jan-2017 18:30:00
I have tried this method 我尝试过这种方法
dateTime = moment("2017-01-09T18:30:00.000Z").format("DD-MMM-YYYY HH:mm:ss");
But I got output like this 9/1/2017 0:00
What I miss? 但是我得到了这样的输出
9/1/2017 0:00
我想念什么?
The reason you're getting 00:00 is because moment
converts the date object to your timezone. 您获得00:00的原因是因为
moment
将日期对象转换为您的时区。 In order to remove this and format the date without the timezone, use moment.utc()
. 为了删除此内容并格式化不带时区的日期,请使用
moment.utc()
。
Update your fiddle to this: 将您的小提琴更新为:
var dateTime = moment.utc("2017-01-09T18:30:00.000Z").format("DD-MMM-YYYY HH:mm:ss"); document.getElementById('output').innerText = dateTime;
and it will work, outputting: 09-Jan-2017 18:30:00
它会工作,输出:
09-Jan-2017 18:30:00
Try using: 尝试使用:
dateTime = moment("2017-01-09T18:30:00.000Z").format("DD-MMM-YYYY HH:mm:ss"); // note the extra Y in YYYY
console.log(dateTime);
// => 10-Jan-2017 05:30:00
For the most part it looks like what you are doing is right. 在大多数情况下,您似乎在做正确的事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.