简体   繁体   English

moment.js 带时区的严格模式时间格式验证

[英]moment.js strict mode time format validation with time zone

I'm trying to get moment.js to validate (strict mode) a user-entered date/time with time zone.我正在尝试让 moment.js 验证(严格模式)用户输入的带时区的日期/时间。 Example:例子:

moment("2018-01-20 06:30 PDT", "YYYY-MM-DD HH:mm Z", true).isValid();

This returns false, though the user-entered value is valid for my purposes.这将返回 false,尽管用户输入的值对我来说是有效的。 If I take off the time zone and the "Z" token it validates fine, as expected.如果我取消时区和“Z”令牌,它会按预期正常验证。

I've checked what seemed the most relevant SO posts but come up empty.我检查了看起来最相关的 SO 帖子,但结果是空的。 I'd appreciate any tips.我很感激任何提示。 Do I need to use moment.tz in some form for this use case?对于这个用例,我需要以某种形式使用 moment.tz 吗? Thanks much in advance.非常感谢。

EDIT: to clarify, "2018-01-20 06:30 PDT" is what the user enters manually, "yyyy-MM-dd HH:mm z" is the (ISO 8601?) date/time format required by the app, and we massage those tokens to "YYYY-MM-DD HH:mm Z" to pass to moment.js for validation.编辑:澄清一下,“2018-01-20 06:30 PDT”是用户手动输入的内容,“yyyy-MM-dd HH:mm z”是应用程序所需的(ISO 8601?)日期/时间格式,我们将这些令牌传递给“YYYY-MM-DD HH:mm Z”以传递给 moment.js 进行验证。

To make it work instead of PDT you can provide timezone offset ie -0700 example -要使其工作而不是PDT,您可以提供时区偏移,即-0700示例 -

moment("2018-01-20 06:30 -0700", "YYYY-MM-DD HH:mm Z", true).isValid();

For more details refer - https://momentjs.com/docs/#/parsing/string-format有关更多详细信息,请参阅 - https://momentjs.com/docs/#/parsing/string-format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM