{

Dart| Flutter: How to Convert timestamp epoch to DateTime local and UTC| Flutter By Example


Timestamp alias Epoch timestamp or Unix timestamp is a long number that represents the number of milliseconds since 1970-01-01 PST. It is a Count of milliseconds elapsed since 1970-01-01 PST. Sometimes, you need to convert timestamp to DateTime object in dart and flutter

You can check how to get Current Timestamp epcho

How to Convert Timestamp to DateTime in Dart and Flutter?

This example converts timestamp in milli and microseconds to DateTime

Let’s see how to get the Current timestamp in milli and microseconds.

void main() {
  print(DateTime.now()); //2022-04-10 11:51:16.566
  print(DateTime.now().millisecondsSinceEpoch); //1649571676566
  print(new DateTime.now().microsecondsSinceEpoch); //1649571676566000
}

DateTime provides two methods fromMicrosecondsSinceEpoch: Creates DateTime object from a given microseconds

DateTime DateTime.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch, {bool isUtc = false})

microsecondsSinceEpoch: The long number represents microseconds elapsed epoch timestamp isUtc: default is false, returns local DateTime, true, returns UTC Date and time. fromMillisecondsSinceEpoch:

DateTime DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, {bool isUtc = false})

millisecondsSinceEpoch: The long number represents milliseconds elapsed epoch timestamp isUtc: false, returns local DateTime, true, returns UTC Date and time.

Here is an example program parse timestamp to Local DateTime

void main() {
  var microSeconds = 1649571676566000;
  var date = new DateTime.fromMicrosecondsSinceEpoch(microSeconds);
  print(date); //2022-04-10 11:51:16.566

  var milliSeconds = 1649571676566;
  var date1 = DateTime.fromMillisecondsSinceEpoch(milliSeconds);
  print(date1); //2022-04-10 11:51:16.566
}

Output

2022-04-10 11:51:16.566
2022-04-10 11:51:16.566

Let’s see an example convert timestamp to Datetime UTC timestamp fromMicrosecondsSinceEpoch and fromMillisecondsSinceEpoch methods accept with isUtc:true argument to return UTC timezone

void main() {
  var microSeconds = 1649571676566000;
  var date = new DateTime.fromMicrosecondsSinceEpoch(microSeconds, isUtc: true);
  print(date); //2022-04-10 06:21:16.566Z

  var milliSeconds = 1649571676566;
  var date1 = DateTime.fromMillisecondsSinceEpoch(milliSeconds, isUtc: true);
  print(date1); //2022-04-10 06:21:16.566Z
}

Output:

2022-04-10 06:21:16.566Z
2022-04-10 06:21:16.566Z

In the output, the Z format in Datetime represents the ISO8601 Datetime standard for UTC dates and times.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example