An instant in time, such as July 20, 1969, 8:18pm GMT.
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.
Create a DateTime object by using one of the constructors or by parsing a correctly formatted string, which complies with a subset of ISO 8601. Note that hours are specified between 0 and 23, as in a 24-hour clock. For example:
var now = new DateTime.now(); var berlinWallFell = new DateTime.utc(1989, 11, 9); var moonLanding = DateTime.parse("1969-07-20 20:18:04Z"); // 8:18pm
A DateTime object is anchored either in the UTC time zone or in the local time zone of the current computer when the object is created.
Once created, neither the value nor the time zone of a DateTime object may be changed.
You can use properties to get the individual units of a DateTime object.
assert(berlinWallFell.month == 11); assert(moonLanding.hour == 20);
For convenience and readability, the DateTime class provides a constant for each day and month name - for example, august and friday. You can use these constants to improve code readability:
var berlinWallFell = new DateTime.utc(1989, DateTime.november, 9); assert(berlinWallFell.weekday == DateTime.thursday);
Day and month values begin at 1, and the week starts on Monday. That is, the constants january and monday are both 1.
A DateTime object is in the local time zone unless explicitly created in the UTC time zone.
var dDay = new DateTime.utc(1944, 6, 6);
Use isUtc to determine whether a DateTime object is based in UTC. Use the methods toLocal and toUtc to get the equivalent date/time value specified in the other time zone. Use timeZoneName to get an abbreviated name of the time zone for the DateTime object. To find the difference between UTC and the time zone of a DateTime object call timeZoneOffset.
The DateTime class contains several handy methods, such as isAfter, isBefore, and isAtSameMomentAs, for comparing DateTime objects.
assert(berlinWallFell.isAfter(moonLanding) == true); assert(berlinWallFell.isBefore(moonLanding) == false);
Use the add and subtract methods with a Duration object to create a new DateTime object based on another. For example, to find the date that is sixty days (24 * 60 hours) after today, write:
var now = new DateTime.now(); var sixtyDaysFromNow = now.add(new Duration(days: 60));
To find out how much time is between two DateTime objects use difference, which returns a Duration object:
var difference = berlinWallFell.difference(moonLanding); assert(difference.inDays == 7416);
The difference between two dates in different time zones is just the number of nanoseconds between the two points in time. It doesn't take calendar days into account. That means that the difference between two midnights in local time may be less than 24 hours times the number of days between them, if there is a daylight saving change in between. If the difference above is calculated using Australian local time, the difference is 7415 days and 23 hours, which is only 7415 whole days as reported by inDays
.
See Duration to represent a span of time. See Stopwatch to measure timespans.
The DateTime class does not provide internationalization. To internationalize your code, use the intl package.
microsecondsSinceEpoch
. [...] millisecondsSinceEpoch
. [...] 1..31
. [...] 0..23
. [...] DateTime
is set to UTC time. [...] 0...999
. [...] 0...999
. [...] 0...59
. [...] 1..12
. [...] 0...59
. [...] duration
added to this. [...] other
, returning zero if the values are equal. [...] other
. [...] other
. [...] other
. [...] other
. [...] duration
subtracted from this. [...] other
is a DateTime at the same moment and in the same time zone (UTC or local). [...] formattedString
. [...] 4
8
7
12
2
5
1
7
6
3
5
1
12
11
10
6
9
7
4
2
3
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-core/DateTime-class.html