在dart中解析任意日期格式 [英] Parsing an arbitrary date format in dart

查看:192
本文介绍了在dart中解析任意日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应接受任意格式日期的应用程序-换句话说,如果它走得像日期,说话像一个日期,那是一个日期。


我试图改写一下moment.js包中的日期格式解析器,以生成部分成功的格式字符串,但是遇到了其中输入字符串包含某些无法识别的元素的情况。


例如,似乎没有合适的格式来处理字符串


2019年11月7日星期四GMT-0800(PST)

code>


-至少对我来说似乎很清楚,但也许我在文档中缺少某些内容。


我看过 https://pub.dev/documentation/intl/ Latest / intl / DateFormat-class.html ,但是我看不到任何方法,例如忽略可能不相关的字段或正确编码格式(例如 GMT -0800 字段)。


如果需要的话,我正在使用 jiffy 包进行日期解析。


跟进:我怀疑我正在寻找的东西或多或少像C语言中的 strptime()一样。

解决方案

我在cseder的评论中提到的基本答案是封装该日期的无用标记。


例如,格式


EEE MMM DD yyyy H:mm: ss'GMT-0800''(PST)'


愉快地解析字符串


2019年11月7日星期四13:50:03 GMT-0800(PST)


我可能还可以处理时区问题,但这解决了眼前的问题。 / p>

I am developing an application that should accept arbitrarily formatted dates -- in other words, if it walks like date, talks like a date, it's a date.

I have attempted to adapt the date format parser from the moment.js package to generate a format string with some partial success but have run into a situation where the input string in it has some element that is not recognized.

For instance, there seems to be no proper format to deal with the string

Thu Nov 07 2019 13:50:03 GMT-0800 (PST)

-- or, at least one which seems clear to me but perhaps I'm missing something in the documentation.

I have looked at https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html but I see no way of, for instance, ignoring fields that may be irrelevant or encoding the format correctly (for instance the GMT-0800 field).

I am using the jiffy package to do the date parsing if that matters.

Following up: I suspect what I'm looking for is something that acts more or less like strptime() in the C language. Or maybe not.

解决方案

The basic answer, as I mentioned in the comment to cseder was to encapsulate the uninteresting tokens of the date string in single quotes, per the documentation.

For instance, the format

EEE MMM DD yyyy H:mm:ss 'GMT-0800' '(PST)'

happily parses the string

Thu Nov 07 2019 13:50:03 GMT-0800 (PST)

I can probably also deal with the timezone issues but this reqolves the immediate question.

这篇关于在dart中解析任意日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆