简体   繁体   English

使用moment.js将日期时间格式化为DD-MMM-YYY HH:mm:ss

[英]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.

相关问题 如何使用moment.js将秒传输到时间格式(YYYY:MM:DD HH:mm:ss) - How to transfer seconds to Time format (YYYY:MM:DD HH:mm:ss) using moment.js 使用moment.js将秒转换为hh:mm:ss格式 - Convert seconds into hh:mm:ss format using moment.js 使用 Iso yyyy-MM-dd HH:mm:ss 的无效日期 moment.js - Invalid Date moment.js using Iso yyyy-MM-dd HH:mm:ss Moment.js以dd:hh:mm:ss格式获取两个日期之间的差异? - Moment.js Get difference between two dates in the dd:hh:mm:ss format? 将日期格式从dd / MM / yyy hh:mm:ss格式化为yyyy / MM / dd hh:mm:ss Angular JS / JavaScript - Format date from dd/MM/yyy hh:mm:ss to yyyy/MM/dd hh:mm:ss Angular JS / JavaScript 使用moment.js(时区)时以(“ YYYY-MM-DD HH:mm”)格式返回日期和时间 - Return date and time in (“YYYY-MM-DD HH:mm”) format when using moment.js (timezone) 当日期为MM / DD / YYYY HH:mm:ss时,moment.js从现在的日期返回意外 - moment.js returning an unexpected fromNow date when date is MM/DD/YYYY HH:mm:ss 使用Moment.js将持续时间格式化为dd:hh:mm格式-错误 - Formatting time-duration into dd:hh:mm format using moment.js - error 使用moment.js将秒持续时间格式化为DD:HH:mm格式 - Formatting seconds duration into DD:HH:mm format by using moment.js 使用 moment.js 将时间 (HH:mm:ss) 转换为 (HH:mm) - Using moment.js to convert time (HH:mm:ss) to (HH:mm)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM