Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Dates


Share

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.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.