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?


Solution 1: sj95126

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.


Solution 2: Manpreet Singh Pandher

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

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