简体   繁体   English

如何使用moment.js将ISO日期/时间转换为本地日期/时间? 接收奇数输出

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

相关问题 Moment.js:将UTC日期从数据库转换为本地时间 - Moment.js: Convert UTC date from database into local time 在javascript(moment.js)中将UTC日期转换为当地时间 - convert UTC date to local time in javascript (moment.js) 我如何在 javascript 中使用 moment.js/Date 将 EST 时间数据转换为本地时间 - how I convert EST time data to local time using moment.js / Date in javascript 使用 Moment.js 将 UTC 日期和时间转换为本地日期和时间 - Convert UTC Date and Time to local Date and Time using Moment.js 如何使用moment.js和moment-timezone.js将特定时区(非本地)的给定日期/时间转换为UTC - How to convert a given date/time for a specific timezone (not local) to UTC using moment.js and moment-timezone.js 我需要使用 moment.js 根据本地时区转换 UTC 日期和时间,如何将日期从 UTC 转换为 html 中的时区 - I need to convert UTC date and time based on the local timezone using moment.js, how to convert date from UTC to timezone in html 使用moment.js转换XML解析的时间和日期的时区 - Convert time zone of XML parsed time and date with moment.js 使用moment.js处理本地化日期以将其转换为本地日期/时间 - Processing localised date to convert it to local date/time using moment.js Moment.js 不显示本地日期和时间格式 - Moment.js not displaying local Date and Time format 使用时区将日期和时间字符串转换为moment.js - Convert date & time string to moment.js with timezone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM