181

I've been learning Dart, but I don't know how to generate a timestamp. I have tried this:

void main() {
  print((new Date()).millisecondsSinceEpoch);
}

Thanks to the IDE I was able to get this far, but I'm getting a confusing error:

Exception: No such method: 'Date'

Help?

Shailen Tuli
  • 13,815
  • 5
  • 40
  • 51

7 Answers7

353

You almost had it right. You just did not use a named constructor:

void main() {
  print(DateTime.now().millisecondsSinceEpoch);
}

Gives:

1351441456747

See the API documentation for more: https://api.dart.dev/stable/2.10.1/dart-core/DateTime-class.html

Tristan Bennett
  • 746
  • 1
  • 8
  • 18
Kai Sellgren
  • 27,954
  • 10
  • 75
  • 87
  • 1
    Is this UTC result? – Jaimin Modi Jan 16 '23 at 07:02
  • `DateTime.now()` gives a moment in time in the local timezone and the first of January 1970, with which it is compared for calculating `millisecondsSinceEpoch` is in UTC. So the result is the moment in time as a UNIX timestamp, this does not have any timezone information associated since it is a number. It is up to you to convert the resulting moment in time back to a local time in a specific timezone. – xuiqzy Jul 01 '23 at 17:36
  • *it gives a moment in time together with the associated local timezone in which it was created – xuiqzy Jul 01 '23 at 18:16
11

This is my solution

DateTime _now = DateTime.now();
print('timestamp: ${_now.hour}:${_now.minute}:${_now.second}.${_now.millisecond}');
Mohsen Emami
  • 2,709
  • 3
  • 33
  • 40
8

In order to get the timestamp in milliseconds from DateTime. Just use the millisecondsSinceEpoch property. Remember that this isn't a callable function rather a property.

DateTime time = DateTime.now();
time.millisecondsSinceEpoch;

January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch. So, you'll get a number which corresponds to the milliseconds passed since Epoch. There is also a microsecondsSinceEpoch property.

Dart Documentation - https://api.dart.dev/stable/2.17.0/dart-core/DateTime/millisecondsSinceEpoch.html

5

Microseconds is also available natively from Dart: (no need to import packages).

void main() {
  print(new DateTime.now().microsecondsSinceEpoch);
}

output:

1591457696860000

Arthur Zennig
  • 2,058
  • 26
  • 20
  • 1
    That comment should not stand as an answer, add a comment to the main answer or request an edit. Right now - it's duplicated. – genericUser Apr 18 '21 at 15:51
1

void main() { print(DateTime.now().millisecondsSinceEpoch); }

Rajni Gujarati
  • 2,709
  • 1
  • 10
  • 16
0

You can simply print the current timestamp in your timezone as a string like so:

print(DateTime.now().toString());

DateTime.Now() also has other methods like .day, .hour, .minute, etc. .millisecondsSinceEpoch is one way to get it independent of timezone - this is how many milliseconds have passed since the "Unix epoch" 1970-01-01T00:00:00Z (UTC).

0

The answer may be posted very late it may help others. I have tried all the above answers which work perfectly to get the time stamp. But, to get your current time need to do some extra work you need to divide with 1000.

DateTime.now().millisecondsSinceEpoch / 1000