Table of contents
1.
Introduction
2.
Constructors of DateTime in Dart
3.
Properties of DateTime in Dart
4.
Methods of DateTime
5.
Frequently Asked Questions
5.1.
Can we check if the two objects have the exact date and time?
5.2.
What is Duration?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Date and Time in Dart

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we are going to learn about the DateTime class in dart. Dart provides us with a beneficial built-in class to deal with the date and time. This class has a lot of constructors, properties, and methods to access the date and time. The date and time we get are either in the form of the UTC convention or maybe in the local time zone of the computer when the object is created. Well, that depends upon the constructor that we use to create the object.

Constructors of DateTime in Dart

DateTime class provides us with a number of constructors to create the objects using different time frames, such as:

a. DateTime.now constructor creates an instance of DateTime using the current date and time in the present time zone.

Syntax:

DateTime.now();


b. DateTime constructor is used to creating an instance, using the specified year, month, day, etc.

Syntax:

DateTime(int year, [int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0]);


c. DateTime.utc constructor does the same thing as the above constructor but uses the UTC time zone.

Syntax:

DateTime.utc(int year, [int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0]);  


d. Now, there are two constructors which create instances using the milli/ microseconds since the epoch. Another parameter tells us about the time zone, whether it is in UTC or local time zone.

Syntax:

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

//And

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


Example: In this example, we are using all the constructors discussed above.

Code:

void main()
{
  // Creating an instance of current date and time. 
  var curr = DateTime.now();
  print("Current data: $curr");
  
  // Specify the date as a UTC time.
  var utc = DateTime.utc(2022);
  print("UTC data: $utc");


  // Create a new DateTime with the local time zone.
  var local = DateTime(1999); // January 1, 2000
  print("Local time: $local");


  // Specifying the year, month and day.
  local = DateTime(1999, 10, 14);
  print("Local time with more params: $local");


  // Specifying a date and time in micro sec. since the Unix epoch.
  var epoch = DateTime.fromMillisecondsSinceEpoch(946684800000,isUtc: true);
  print("Time since epoch: $epoch");
}


Output:

Current data: 2022-05-01 15:56:35.652
UTC data: 2022-01-01 00:00:00.000Z
Local time: 1999-01-01 00:00:00.000
Local time with more params: 1999-10-14 00:00:00.000
Time since epoch: 2000-01-01 00:00:00.000Z


Explanation: Here, we made three objects "curr" which has the current date and time, "utc" which has the UTC time, "local" which shows the local time and "epoch" which shows the date time million seconds since epoch.

Properties of DateTime in Dart

There are some properties with which the dart DateTime class provides us to know some information about the instance we have created, such as:

Example: In this example, we are using all the properties discussed above.

Code:

void main()
{
  // Creating an instance of current date and time. 
  var curr = DateTime.now();
  print("Current data: $curr");


  // Specifying the year, month, day, hour, minute, second.
  var local = DateTime(1999, 10, 14, 12, 30, 45);
  print("Local time: $local");
  print("Information of DateTime object.");
  print("Day: ${local.day}");
  print("Hour: ${local.hour}");
  print("Is the time is in UTC: ${local.isUtc}");
  print("Millisecond: ${local.millisecond}");
  print("Time since epoch is: ${local.microsecondsSinceEpoch}");
  print("Minute: ${local.minute}");
  print("Month: ${local.month}");
  print("Weekday: ${local.weekday}");
  print("Second: ${local.second}");
  print("Year: ${local.year}");
}


Output:

Current data: 2022-05-01 16:23:48.592
Local time: 1999-10-14 12:30:45.000
Information of DateTime object.
Day: 14
Hour: 12
Is the time is in UTC: false
Millisecond: 0
Time since epoch is: 939884445000000
Minute: 30
Month: 10
Weekday: 4
Second: 45
Year: 1999


Explanation: Here, we create two objects, "curr" and "local". On the "local" object, we apply all the properties. 

Methods of DateTime

Now we have some methods, and these methods are used to manipulate the created DateTime object. Dart provides us with a number of methods to use for the DateTime object here. We are discussing some of the essential methods, the rest you find on dart's docs.

Example: In this example, we are using all the methods discussed above.

Code:

// In this example we are using add(), subtract(), difference(), 
// isAfter(), isBefore() and compareTo() methods
void main()
{
  // Specifying the year, month, day, hour, minute, second.
  var local = DateTime(1999, 11, 14, 12, 30, 45);
  print("Local time: $local\n");
  
  // Here we are adding 40 days and 12 hours to “local” object.
  var localAdded = local.add(Duration(days:40, hours:12));
  print("After adding 40 days and 12 hours: $localAdded");
  print("Is the new date is after: ${localAdded.isAfter(local)}\n");
  
  // Here we are subtracting 20 days and 10 hours to local object.
  var localSubtracted = local.subtract(Duration(days:20, hours:10));
  print("After subtracting 20 days and 10 hours: $localSubtracted");
  print("Is the new date is before: ${localSubtracted.isBefore(local)}\n");
  
  // Here we are using the compareTo() method, if the method is not 
  // returning zero then it means that both the objects are not equal. 
  if(localAdded.compareTo(localSubtracted) != 0)
  {
    // Now let's check the duration between both the newly created 
    // object localAdded and localSubtracted.
    var dur = localAdded.difference(localSubtracted);
    print("The duration between dates after adding and subtracting: ${dur.inDays}");
  }
}


Output:

Local time: 1999-11-14 12:30:45.000

After adding 40 days and 12 hours: 1999-12-25 00:30:45.000
Is the new date is after: true

After subtracting 20 days and 10 hours: 1999-10-25 02:30:45.000
Is the new date is before: true

The duration between dates after adding and subtracting: 60

Explanation: Here, we first created an object, then we added 40 days and 12 hours to that object and stored its value to a new object "localAdded" and also we subtracted 20 days and 12 hours from that "local" object and stored the value to "localSubtracted". Then we compared both the newly created objects.

Frequently Asked Questions

Can we check if the two objects have the exact date and time?

Yes, dart's DateTime class provides us a method that checks whether two objects have the exact date and time is isAtSameMomentAs(), and as a result, it returns a bool type object. 

What is Duration?

Just like the DateTime, Duration is also a class in dart whose object gives us a duration of time. Its constructor takes days, hours, minutes, and seconds to create an object.  

 Is there any way to interpret the result of the duration class?

The duration class has provided us with a number of properties, and using these properties, we can simplify the duration object result. Some of the properties are inDays, inHours, inMinutes, inSeconds, etc.

Conclusion

In this article, we've extensively discussed the DateTime class in dart and its implementation. We have discussed how to use its constructors, properties, and methods.

We hope this blog has helped you enhance your knowledge regarding the DateTime class of dart. Do check out the awesome content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass