[英]How to convert ISO date/time to local date/time with moment.js? Receiving odd output
I'm using moment.js to convert from an ISO date/time/zone string to a local one. 我正在使用moment.js将ISO日期/时间/区域字符串转换为本地字符串。 Based on the docs and other similar questions such as this , what should be pretty straightforward is turning out not to be, and is giving me some odd output. 基于文档和其他类似问题,例如,应该很简单, 事实并非如此,并且给了我一些奇怪的输出。 Here is what I have: 这是我所拥有的:
console.log('date/time before is: ', date);
// date/time before is: 2016-12-23T23:10:00.000Z
var datetime = moment(date).format("dddd, MMMM Do YYYY, h:mm:ss a");
console.log('date/time after is: ', datetime);
// date/time after is: pátek, prosinec 23. 2016, 3:10:00 pm
The format string I am using is directly from the docs. 我使用的格式字符串直接来自文档。 The intent is to be able to format it in the way I need it once I get it working. 目的是一旦我可以使用它,就可以按照我需要的方式对其进行格式化。
I'm guessing that you are using moment-with-locales, because "pátek, prosinec" is Czech for "Friday, December". 我猜您使用的是“随地而动”,因为“pátek,prosinec”是捷克语,意为“ 12月星期五”。
I used the following cdn link for moment.js: 我为moment.js使用了以下cdn链接:
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js
And this code: 这段代码:
var date = new Date().toISOString();
console.log(date);
var datetime = moment(date).format("dddd, MMMM Do YYYY, h:mm:ss a");
console.log(datetime);
And got the expected result. 并取得了预期的结果。 Here's a fiddle using moment.js that produces the output I think you want. 这是一个使用moment.js的小提琴 ,它产生了我认为想要的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.