Dart Dates
Dart Dates
Dart has a built-in class called DateTime for dates and times.
It is part of dart:core, so you do not need an import.
A DateTime object holds a year, month, day, hour, minute, second and millisecond.
Create a Date
To create a date, write the year, the month and the day:
Example
void main() {
DateTime birthday = DateTime(2026, 1, 15);
print(birthday);
DateTime meeting = DateTime(2026, 3, 10, 14, 30);
print(meeting);
}
Try it Yourself »
Result:
2026-01-15 00:00:00.000
2026-03-10 14:30:00.000
The hour, minute and second are optional.
If you leave them out, the time is midnight (00:00:00).
Months are numbered from 1 (January) to 12 (December).
Parts of a Date
You can get each part of a date with properties like year, month and day.
The weekday property is a number from 1 (Monday) to 7 (Sunday):
Example
void main() {
DateTime meeting = DateTime(2026, 3, 10, 14, 30);
print(meeting.year);
print(meeting.month);
print(meeting.day);
print(meeting.hour);
print(meeting.minute);
print(meeting.weekday);
}
Try it Yourself »
Result:
2026
3
10
14
30
2
March 10, 2026 is a Tuesday, so weekday is 2.
The Current Date and Time
DateTime.now() returns the date and time right now, on the computer that runs the program.
The output changes every time you run the code, so this is only an example of what it looks like:
DateTime now = DateTime.now();
print(now);
Result:
2026-11-19 14:05:32.481193
The example below only prints values that do not change, so you can compare it with your own result:
Example
void main() {
DateTime now = DateTime.now();
print(now.isAfter(DateTime(2020, 1, 1)));
print(now.year >= 2026);
}
Try it Yourself »
Result:
true
true
Read a Date From a String
DateTime.parse() turns a string into a DateTime.
The string should use the format year-month-day, with an optional time after it:
Example
void main() {
DateTime start = DateTime.parse('2026-07-01');
DateTime flight = DateTime.parse('2026-07-01 08:45:00');
print(start);
print(flight);
print(flight.hour);
}
Try it Yourself »
Result:
2026-07-01 00:00:00.000
2026-07-01 08:45:00.000
8
If the string is not a valid date, parse() throws a FormatException.
Use DateTime.tryParse() if you want null instead of an exception:
Example
void main() {
DateTime? date = DateTime.tryParse('next Friday');
print(date);
}
Try it Yourself »
Result:
null
Add, Subtract and Difference
A Duration is a length of time, like 5 days or 3 hours.
Use add() and subtract() to move a date forward or back:
Example
void main() {
DateTime order = DateTime(2026, 5, 28);
DateTime delivery = order.add(Duration(days: 5));
DateTime reminder = order.subtract(Duration(hours: 12));
print(delivery);
print(reminder);
}
Try it Yourself »
Result:
2026-06-02 00:00:00.000
2026-05-27 12:00:00.000
Notice that Dart moves to the next month by itself.
The difference() method returns the Duration between two dates:
Example
void main() {
DateTime today = DateTime(2026, 11, 19);
DateTime newYear = DateTime(2027, 1, 1);
Duration left = newYear.difference(today);
print('Days until New Year: ${left.inDays}');
print('Hours until New Year: ${left.inHours}');
}
Try it Yourself »
Result:
Days until New Year: 43
Hours until New Year: 1032
Compare Dates
Use isBefore(), isAfter() and isAtSameMomentAs() to compare two dates:
Example
void main() {
DateTime deadline = DateTime(2026, 6, 30);
DateTime sent = DateTime(2026, 6, 28);
print(sent.isBefore(deadline));
print(sent.isAfter(deadline));
print(sent.isAtSameMomentAs(DateTime(2026, 6, 28)));
if (sent.isBefore(deadline)) {
print('The homework is on time');
}
}
Try it Yourself »
Result:
true
false
true
The homework is on time
Format a Date
Dart has no built-in way to write a date in a format like 15/01/2026.
You can build the string yourself with the parts of the date.
The padLeft(2, '0') method adds a zero in front of numbers that have only one digit:
Example
void main() {
DateTime date = DateTime(2026, 1, 5, 9, 7);
String day = date.day.toString().padLeft(2, '0');
String month = date.month.toString().padLeft(2, '0');
String hour = date.hour.toString().padLeft(2, '0');
String minute = date.minute.toString().padLeft(2, '0');
print('$day/$month/${date.year}');
print('$hour:$minute');
List<String> months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
print('${months[date.month - 1]} ${date.day}, ${date.year}');
}
Try it Yourself »
Result:
05/01/2026
09:07
Jan 5, 2026
Tip: Many Dart apps use the intl package to format dates in different languages.