问题
I am using the Twitter api for getting tweets in a Flutte App. The api returns a formatted date like this:
Wed Jun 12 00:08:35 +0000 2019
DateTime formatTwitterDate() {
final format = DateFormat('EEE MMM dd hh:mm:ss +0000 yyyy'); //todo failed to resolve +0000
return format.parse(this);
}
This is the only formatter I got working. How can I support +0000?
回答1:
Used this javascript implementation and transformed it to a DartExtension
https://stackoverflow.com/a/2611438/5115254
extension StringToDateTime on String {
DateTime formatTwitterDate() {
final newDateString = '${replaceAll('+0000 ', '')} UTC';
final format = DateFormat('EEE MMM dd hh:mm:ss yyyy Z');
return format.parse(newDateString);
}
}
回答2:
Porting this answer to dart
var s = "Fri Apr 09 12:53:54 +0000 2010";
print(s);
final newString =
s.replaceAllMapped(RegExp(r'\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)'), (m) {
return "${m[1]} ${m[2]} ${m[4]} ${m[3]} UTC";
});
print(newString);
来源:https://stackoverflow.com/questions/61519435/dart-parse-date-with-0000