我正在使用moment.js和moment-timezone来获取本地utc日期字符串。 我已经创建了下面的函数来实现这一目标。 在处理阿拉伯语语言环境时尝试使用此功能时,我将返回无效日期。
function _getLocalUtcDateString(utcDateString, timezoneId) { var utcMoment = moment.utc(utcDateString, 'MM/DD/YYYY hh:mm:ss A').format('MM/DD/YYYY hh:mm:ss A'); var localTimeTz = moment.utc(utcMoment).tz(timezoneId).format('MM/DD/YYYY hh:mm:ss A'); return localTimeTz; }如果我使用以下参数_getLocalUtcDateString("11/2/2016 4:45:47 PM", "America/New_York")调用它并设置moment.locale('ar')则失败且日期无效。 问题似乎在于utcMoment ,当lacale是阿拉伯语时,这个值等于١١/٠٢/٢٠١٦ ٠٤:٤٥:٤٧ ص ,这导致了moment.utc(utcMoment).tz(timezoneId).format('MM/DD/YYYY hh:mm:ss A'); 失败。
I'm using moment.js and moment-timezone to get a local utc date string. I've created the function below to achieve this. When trying to use this when dealing with an Arabic locale I'm getting invalid date returned.
function _getLocalUtcDateString(utcDateString, timezoneId) { var utcMoment = moment.utc(utcDateString, 'MM/DD/YYYY hh:mm:ss A').format('MM/DD/YYYY hh:mm:ss A'); var localTimeTz = moment.utc(utcMoment).tz(timezoneId).format('MM/DD/YYYY hh:mm:ss A'); return localTimeTz; }If I call it with the following parameters _getLocalUtcDateString("11/2/2016 4:45:47 PM", "America/New_York") and set moment.locale('ar') it fails with invalid date. The problem seems to be with utcMoment, when the lacale is Arabic this value equals ١١/٠٢/٢٠١٦ ٠٤:٤٥:٤٧ ص which is causing moment.utc(utcMoment).tz(timezoneId).format('MM/DD/YYYY hh:mm:ss A'); to fail.
最满意答案
如果您打开控制台,您将看到以下警告消息:
弃用警告:提供的值不是公认的ISO格式。 时刻构造回落到js Date(),这在所有浏览器和版本中都不可靠。 不鼓励使用非ISO日期格式,将在即将发布的主要版本中删除。 有关更多信息,请参阅http://momentjs.com/guides/#/warnings/js-date/ 。
所以问题是您正在尝试解析不是可识别的ISO格式的字符串(请参阅解析文档以获取其他信息)。
要解决此问题,您只需删除函数第一行中不必要的format() ,如以下工作代码段所示:
function _getLocalUtcDateString(utcDateString, timezoneId) { var utcMoment = moment.utc(utcDateString, 'MM/DD/YYYY hh:mm:ss A'); var localTimeTz = moment.utc(utcMoment).tz(timezoneId).format('MM/DD/YYYY hh:mm:ss A'); return localTimeTz; } moment.locale('ar'); var res = _getLocalUtcDateString("11/2/2016 4:45:47 PM", "America/New_York"); console.log(res);<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment-with-locales.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.7/moment-timezone-with-data-2010-2020.min.js"></script>If you open the console, you will see the following warning message:
Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
So the problem is the you are trying to parse a string that isn't in a recognized ISO format (see parsing docs for additional infos).
To fix the issue, you can simply remove the unnecessary format() in the first line of your function, as shown in the following working snippet:
function _getLocalUtcDateString(utcDateString, timezoneId) { var utcMoment = moment.utc(utcDateString, 'MM/DD/YYYY hh:mm:ss A'); var localTimeTz = moment.utc(utcMoment).tz(timezoneId).format('MM/DD/YYYY hh:mm:ss A'); return localTimeTz; } moment.locale('ar'); var res = _getLocalUtcDateString("11/2/2016 4:45:47 PM", "America/New_York"); console.log(res);<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment-with-locales.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.7/moment-timezone-with-data-2010-2020.min.js"></script>
更多推荐
发布评论