Dart Sync

I need to parse such a date "Mon, 11 Aug 2014 12:53 pm PDT" into a DateTime object in a Dart,

DateTime has a static parse method that accepts a subset of the ISO 8601 format, not my case.

The DateFormat class allows DateFormat to define a date pattern for parsing. I created the template "EEE, dd MMM yyyy hh: mm a zzz".

Using it, I get a FormatException: Trying to read a from Mon, 11 Aug 2014 12:53 pm PDT at position 23 .

It seems like the parser doesn't like the case of the PM marker (I opened issue ).

I tried to get around the problem with the top case of the entire line. With the entire uppercase string, I get a FormatException again due to the day of the week and the month names in uppercase.

Any other solution or workaround?

+6
source share
1 answer

You can simply replace the lowercase characters "am" / 'pm "in uppercase.

 import 'package:intl/intl.dart'; void main() { var date = 'Mon, 11 Aug 2014 12:53 pm PDT'; DateFormat format = new DateFormat("EEE, dd MMM yyyy hh:mm a zzz"); date = date.replaceFirst(' pm', ' PM').replaceFirst(' am', ' AM'); print(date); print(format.parse(date)); } 
+8
source

Source: https://habr.com/ru/post/973668/


All Articles