gpt4 book ai didi

javascript - 如何在js-joda中进行严格的LocalDate解析?

转载 作者:行者123 更新时间:2023-12-02 14:39:38 24 4
gpt4 key购买 nike

我正在尝试解析 LocalDateString 使用 js-joda 中的严格解析(版本 1.1.1)。我不想接受无效日期的输入,例如 2016-05-32。但我就是不明白。

我的代码是:

formatter = (new JSJoda.DateTimeFormatterBuilder)
.appendPattern("yyyy-MM-dd")
.toFormatter(JSJoda.ResolverStyle.STRICT);

JSJoda.LocalDate.parse("2016-05-10", formatter);

和错误:

Text '2016-05-10' could not be parsed: Unable to obtain LocalDate from
TemporalAccessor: [object Object], type DateTimeBuilder: 2016-05-10,
at index: 0

使用 ResolverStyle.LENIENTResolverStyle.SMART 的相同代码可以按此模式的预期工作。

formatter = (new JSJoda.DateTimeFormatterBuilder)
.appendPattern("yyyy-MM-dd")
.toFormatter(JSJoda.ResolverStyle.LENIENT);

JSJoda.LocalDate.parse("2016-05-32", formatter); // result 2016-06-01

如何在 js-joda 中使用严格分辨率?

JS Demo

更新:

但是js-joda DateTimeFormatter API没有提到这个选项,@JodaStephen 提出的模式 uuuu-MM-dd 效果很好。 Working Js Demo

js-joda api :

 |Symbol  |Meaning                     |Presentation      |Examples |--------|----------------------------|------------------|--------------- | G      | era                        | number/text      | 1; 01; AD; Anno Domini | y      | year                       | year             | 2004; 04 | D      | day-of-year                | number           | 189 | M      | month-of-year              | number/text      | 7; 07; Jul; July; J

Java api :

 Symbol  Meaning                     Presentation      Examples  ------  -------                     ------------      -------   G       era                         text              AD; Anno Domini; A   u       year                        year              2004; 04   y       year-of-era                 year              2004; 04   D       day-of-year                 number            189

最佳答案

模式“yyyy-MM-dd”在 STRICT 模式下不完整,因为它不提供时代。请改用“uuuu-MM-dd”模式。

关于javascript - 如何在js-joda中进行严格的LocalDate解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37125709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com