0

I am using dart/flutter and need some help on how format date . How do I parse the following string to a timestamp

2019-08-22T00:40:57.166+0000

Basically, I want to get the timestamp for the above date and pass it to timeago to format

Thanks

user2570135
  • 2,669
  • 6
  • 50
  • 80
  • [`DateTime.parse`](https://api.dartlang.org/stable/2.5.0/dart-core/DateTime/parse.html)/[`DateTime.tryParse`](https://api.dartlang.org/stable/2.5.0/dart-core/DateTime/tryParse.html)? – jamesdlin Sep 11 '19 at 04:12
  • 1
    Possible duplicate of [convert datetime string to datetime object in dart?](https://stackoverflow.com/questions/49385303/convert-datetime-string-to-datetime-object-in-dart) – Tokenyet Sep 11 '19 at 05:02

1 Answers1

0

This code will take your string and convert it to milliseconds since epoch (I assume that's what you mean by timestamp:

String dateStr = '2019-08-22T00:40:57.166+0000';
DateTime date = DateTime.parse(dateStr);
print("The timestamp for date '$dateStr' is ${date.millisecondsSinceEpoch}");

The output of the above program is:

The timestamp for date '2019-08-22T00:40:57.166+0000' is 1566434457166

Good luck! :)

Mark Madej
  • 1,752
  • 1
  • 14
  • 19