Home > Mobile >  Dart converting int? timestamp to date gives me wrong date
Dart converting int? timestamp to date gives me wrong date

Time:07-26

I have a timestamp like so:

int? completedTimestamp; //1657948481451

When I try to convert this to a date like so:

var date = DateTime.fromMicrosecondsSinceEpoch(completedTimestamp!, isUtc: true);

date returns this:

1970-01-20 04:32:28

I am expecting 2022-07-16 5:14:41

What am I doing wrong here?

CodePudding user response:

You need to convert timestamp with milliseconds to date as below:

 var date = DateTime.fromMillisecondsSinceEpoch(completedTimestamp!, isUtc: true);

CodePudding user response:

The timestamp you're using is a count of milliseconds since the epoch, so you want to use fromMillisecondsSinceEpoch(), not fromMicrosecondsSinceEpoch().

DateTime.fromMillisecondsSinceEpoch() will return the correct timestamp of 2022-07-16 05:14:41.451Z.

  • Related