Dart DateTime.now()
Syntax & Examples


Syntax of DateTime.now

The syntax of DateTime.DateTime.now constructor is:

DateTime.now()

This DateTime.now constructor of DateTime constructs a DateTime instance with current date and time in the local time zone.



✐ Examples

1 Get current date and time in local time zone

In this example,

  1. We use the DateTime.now() constructor to create a DateTime object representing the current date and time in the local time zone.
  2. We print the DateTime object to standard output.

Dart Program

void main() {
  var now = DateTime.now();
  print(now);
}

Output

2024-05-03 10:30:00.000

2 Get current date and time in local time zone

In this example,

  1. We use the DateTime.now() constructor to create a DateTime object representing the current date and time in the local time zone.
  2. We print the DateTime object to standard output.

Dart Program

void main() {
  var now = DateTime.now();
  print(now);
}

Output

2024-05-03 10:30:00.000

3 Get current date and time in local time zone

In this example,

  1. We use the DateTime.now() constructor to create a DateTime object representing the current date and time in the local time zone.
  2. We print the DateTime object to standard output.

Dart Program

void main() {
  var now = DateTime.now();
  print(now);
}

Output

2024-05-03 10:30:00.000

Summary

In this Dart tutorial, we learned about DateTime.now constructor of DateTime: the syntax and few working examples with output and detailed explanation for each example.