James Hurford James Hurford - 4 years ago 178
Dart Question

Parse a date string from a twitter post with timezone info included

I have created a tweet using Twitters REST API. The returned JSON from Twitter from the created Tweet is in this format

'Tue Jan 31 11:15:15 +0000 2017'


Darts

DateTime.parse('Tue Jan 31 11:15:15 +0000 2017');


won't parse this format. I turned to the intl package holds some hope in dealing with this. I try to use
DateFormat
to solve this thus

DateFormat format = new DateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");
format.parse('Tue Jan 31 11:15:15 +0000 2017');


It fails. The only way I can get it to work is to remove the timezone part of the twitter date string

'Tue Jan 31 11:15:15 2017'


and do the following

var date = 'Tue Jan 31 11:15:15 +0000 2017'.split(' ');
date.removeAt(4);
DateFormat format = new DateFormat("EEE MMM dd HH:mm:ss yyyy");
format.parse(date.join(' '));


How can I parse a Twitter datetime string without removing the timezone info part?

Answer Source

It looks like package:intl doesn't support timezones yet: https://github.com/dart-lang/intl/issues/128

You could add your use case to the issue (it shouldn't be much work or require extra data to support the 'ZZZZ' case, since you just need to parse a number), or even better, submit a Pull Request!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download