Dart parse date with +0000

懵懂的女人 提交于 2020-05-17 05:46:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!