Dart – Get current DateTime

Dart provides a good DateTime class to simplify the way we work with DateTime. In this post, we will go through an example about current DateTime.

void main() {
  final dateTime = DateTime.now();

  print('Datetime: ${dateTime}');
  print('Iso8601: ${dateTime.toIso8601String()}');
  print('Milliseconds since Epoch: ${dateTime.millisecondsSinceEpoch}');
  print('Seconds since Epoch: ${dateTime.microsecondsSinceEpoch}');
  print('Day: ${dateTime.day}');
  print('Month: ${dateTime.month}');
  print('Year: ${dateTime.year}');
  print('Millisecond: ${dateTime.millisecond}');
  print('Second: ${dateTime.second}');
  print('Minute: ${dateTime.minute}');
  print('Hour: ${dateTime.hour}');
  print('To local: ${dateTime.toLocal()}');
  print('Timezone name: ${dateTime.timeZoneName}');
  print('Timezone offset: ${dateTime.timeZoneOffset}');
  print('Is UTC: ${dateTime.isUtc}');
  print('Weekday: ${dateTime.weekday}');
}

Output

Datetime: 2020-07-30 15:21:19.842261
Iso8601: 2020-07-30T15:21:19.842261
Milliseconds since Epoch: 1596097279842
Seconds since Epoch: 1596097279842261
Day: 30
Month: 7
Year: 2020
Millisecond: 842
Second: 19
Minute: 21
Hour: 15
To local: 2020-07-30 15:21:19.842261
Timezone name: +07
Timezone offset: 7:00:00.000000
Is UTC: false
Weekday: 4
Tagged : /
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x