The DateTime class provides methods to work with dates and times when developing flutter or dart applications. An example could be an instance in time of June 20, 1969, 7:20pm GMT. As the official documentation says “DateTimes can represent time values that are at a distance of at most 100,000,000 days from epoch (1970-01-01 UTC): -271821-04-20 to 275760-09-13.”
How to create a DateTime Object?
To quickly create a DateTime object, you can use one of the constructors available by passing a correctly formatted string. The string mush compiles with a subset of ISO 8601. And also it’s important to note that hours are specified between 0 and 23 just as in a 24-hour clock.
final now = DateTime.now();
final accraBel = DateTime.utc(1989, 11, 9);
final moonLanding = DateTime.parse('1969-07-20 21:20:04Z'); // 9:20pm
Dart/Flutter get the current date and time
Use DateTime.now()
method to get the current date and time. Note that it returns an object of DateTime.
DateTime cur_DateTime = DateTime.now();
print(cur_DateTime);
// result: 2022-02-02 08:48:26.605217
Get The Number of A Particular Day or a Month
Use DateTime.<month-Name or Day Name>
to get the number of that month or a day. it returns an int value which represents the day/month number. Example.
int dayNumber = DateTime.sunday;
print(dayNumber);
// result: 7
int monthNumber = DateTime.december;
print(monthNumber);
// result: 12
final now = DateTime.now();
final berlinWallFell = DateTime.utc(1989, 11, 9);
final moonLanding = DateTime.parse('1969-07-20 20:18:04Z'); // 8:18pm
print(berlinWallFell.year); // 1989
print(berlinWallFell.month); // 11
print(berlinWallFell.day); // 9
print(moonLanding.hour); // 20
print(moonLanding.minute); // 18
ADD & SUBTRACT DAYS, MINUTES, SECONDS, AND MILLISECONDS FROM A PARTICULAR DATE
The Duration()
class gives you access to work with days, hours, minute and seconds. So use the <DateTime>.add(Duration(key:value))
to add days, hours, minutes and seconds to a particular DateTime.
Also, use <DateTime>.subtract(Duration(key:value))
to subtract days, hours, minutes and seconds from a particular DateTime.
DateTime oldDateTime = DateTime.parse("2022-02-02 00:00:00");
// ADD 2 days to the above oldDateTime
DateTime newDate = oldDateTime.add(Duration(days: 2));
print(newDate);
// result: 2022-02-04 00:00:00.000
// two days have been added to the above oldDateTime
// subtract 2 days from the above oldDateTime
DateTime subDate = oldDateTime.subtract(Duration(days: 2));
print(subDate);
// result: 2022-01-31 00:00:00.000
// two days have been subtracted from the above oldDateTime
GET THE YEAR, DAY, MONTH, HOUR, MINUTES, SECONDS of a particular date
Use <particular_DateTime>.T
the "T"
can be year, month, day, hour, minute, seconds
print(oldDateTime.year);
// result: 2022
Working with custom DateTime
Use the DateTime.parse(<datetime-here>)
method and pass in your own date & time. You can separate the date and the time with “T” eg: 2022-02-02T10:02:24.5 the “T” between the character indicates that the string after the date is a time. Or you can simply use space to separate the date and time if you don’t want to use “T” eg: 2022-02-02 10:02:24.500
DateTime customeDateTime = DateTime.parse("2022-02-02T10:02:24.5");
print(customeDateTime);
// result: 2022-02-02 10:02:24.500
Checking the difference between two DateTime in days, hours, minutes, seconds, and Microseconds
The difference()
method returns an object of the Duration of the associated date. So you can use that object to check the difference in days, hours, minutes, seconds and Microseconds.
Duration difference = yesterDayDatetime.difference(todayDatetime);
// difference in days:
print(difference.inDays);
// result: -1
// difference in inHours:
print(difference.inHours);
// result: -24
Checking the negative value difference of two DateTime
DateTime oneDatetime = DateTime.parse("2022-02-02 10:02:24.5");
DateTime twoDayDatetime = DateTime.parse("2022-02-01 10:02:24.5");
Duration diff = yesterDayDatetime.difference(todayDatetime);
print(diff); // result: -24:00:00.000000
// check if the difference is Negative
print(diff.isNegative);
// true
Working with UTC and local time
When you create a DateTime object it will always be in the local time zone of the operating system. For example, if you create an object of a DateTime on the font-end of the app, it means it will be created based on the time zone of the client using the app.
you have to make sure to always explicitly create it in the UTC or GMT time zone.
To check if a DateTime object is in UTC use isUtc to deterine it.
final someDay = DateTime.utc(1944, 6, 6);
print(someDay.isUtc); // true
final someDayLocal = DateTime(1944, 6, 6);
print(someDayLocal.isUtc); // false
To get the equivalent DateTime value that is specified in other time zone, use these two methods toLocal() and to.Utc()
final someDay = DateTime.utc(1944, 6, 6);
final localDay = someDay.toLocal(); // e.g. 1944-06-06 02:00:00.000
print(localDay.isUtc); // false
final utcFromLocal = localDay.toUtc(); // 1944-06-06 00:00:00.000Z
print(utcFromLocal.isUtc);