DateTime class
The DateTime class is used to identify an instantaneous time node, and a time object can be constructed from a string of standard format (ISO 8601 compliant) through a constructor. DateTIme uses 24-hour timing. Here’s a basic 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
Copy the code
The DateTime object is anchored to the Universal Time Coordinated (UTC) Time zone or to the device’s local Time zone. After creation, neither the DateTime value nor the time zone to which it belongs changes. The specific time value can be read from the object property:
assert(berlinWallFell.month == 11); // Assert (moonLanding. Hour == 20); // Moon landing time (hours)Copy the code
For convenience and readability, the DateTIme class provides constant values of the week and month to call. You can use many constants to improve code readability, as shown in the following example:
var berlinWallFell = new DateTime.utc(1989, DateTime.november, 9);
assert(berlinWallFell.weekday == DateTime.thursday);
Copy the code
Both week and month constants start at 1, and the start of the week starts on Monday, so January and Monday are both 1.
A constructor
DateTime(int year, [ int month = 1 int day = 1 int hour = 0 int minute = 0 int second = 0 int millisecond = 0 int microsecond = 0 ]) The method creates a DateTime DateTime instance on the local time zone. FromMicrosecondsSinceEpoch (int microsecondsSinceEpoch, {bool isUtc: False}) a DateTime instance is obtained by entering a microsecond of 00:00:00:00 on January 1, 1970. DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, { bool isUtc: False}) this method inputs the number of milliseconds from 00:00:00:00 on January 1, 1970 to get an instance of DateTime. Datetime.now () This method returns a DateTime instance representing the current time, depending on the local time zone. 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 ]) This method returns a DateTime instance based on UTC
Instance methods
Add (Duration Duration) → DateTime Plus the interval represented by the incoming Duration instance returns a new DateTime instance, with the time difference between the new instance and the calling instance being Duration
var now = DateTime.now();
var span = Duration(days: 1);
print(now);
print(now.add(span));
Copy the code
2019-12-15 09:29:59.605510 2019-12-16 09:29:59.605510 2019-12-15 09:29:59.605510
CompareTo (DateTime other) → int Compares two DateTime instances, if they are equal, then returns 0 difference(DateTime other) → Duration Returns the time difference between the current instance and the incoming instance, Return a Duration instance isAfter(DateTime other) → bool To compare with DateTime passed in, IsAtSameMomentAs (DateTime other) → bool Compares with DateTime. Return true isBefore(DateTime other) → bool For comparison with DateTime; Return true Subtract (Duration Duration) → DateTime The time interval represented by the passed Duration instance is deducted and a new DateTime instance is returned with the time difference between the new instance and the calling instance being Duration
var now = DateTime.now();
var span = Duration(days: 1);
print(now);
print(now.subtract(span));
Copy the code
2019-12-15 09:32:02.811829 2019-12-14 09:32:02.811829
ToIso8601String () → String Returns the time format in full precision according to the ios-8601 standard toLocal() → DateTime Returns the instance represented in the local time zone toString() → String Return string representation of the instance toUtc() → DateTime Returns the instance in UTC time representation
The operator
Operator ==(Dynamic Other) → bool Returns true only if the incoming value is an instance of DateTime and represents the same time in the same time zone
A static method
Parse (String formattedString) → DateTime Constructs an instance of DateTime from a String in standard format
var now = DateTime.parse('2019-12-12');
print(now);
Copy the code
Output: 2019-12-12 00:00:00.000
TryParse (String formattedString) → DateTime Constructs an instance of DateTime from a String in standard format
Instance attributes
Day → int Returns the date of the instance, ranging from 1 to 30 hashCode → int Returns the hash code of the instance object hour → int Returns the time of the hour in the 24-hour timeframe. The value ranges from 0 to 23 isUtc → bool returns whether the current instance isUtc. Microsecond returns the last three digits of the microseconds in which the instance is represented from 00:00 00:00 00:00 January 1, 1970. MicrosecondsSinceEpoch → int (millisecond → int) Millisecond → int (millisecond → int Returns the last three digits of the number of milliseconds representing the time from 00:00:00:00 on January 1, 1970, MillisecondsSinceEpoch – range 0 to 999 int returns the instance of time from January 1, 1970 0 0 0 seconds when the number of milliseconds the minute – int returns the instance says the number of minutes of time, Month → int Specifies the month in which the instance is displayed. The value ranges from 1 to 12. Second → int Specifies the month in which the instance is displayed. TimeZoneName → String returns the timeZoneName of the instance. TimeZoneOffset → Duration returns the interval between the current time zone and UTC time. Duration weekday → int Year → int year of the week
Use UTC time zone and local time zone
DateTime instances default to the local time zone, unless the UTC time zone is declared on display at creation time.
var dDay = new DateTime.utc(1944, 6, 6);
Copy the code
You can use isUtc to determine whether the current time is a UTC time zone. You can use toLocal and toUtc to convert time intervals, use timeZoneName to get the shorthand for the time zone to which the DateTime instance belongs, and use timeZoneOffset to get the time difference between different time zones.
Comparison between DateTime instances
The DateTime class has several simple methods for comparing DateTime instances, such as isAfter, isBefore, and isAtSameMomentAs:
assert(berlinWallFell.isAfter(moonLanding) == true);
assert(berlinWallFell.isBefore(moonLanding) == false);
Copy the code
Use with the DateTime and Duration classes
You can operate on the DateTime instance by adding and subtract a Duration object and return a new instance. For example, calculate today’s time six days from now:
var now = new DateTime.now();
var sixtyDaysFromNow = now.add(new Duration(days: 60));
Copy the code
To find the difference between two DateTime instances, we can use the difference method, which returns a Duration instance:
var difference = berlinWallFell.difference(moonLanding);
assert(difference.inDays == 7416);
Copy the code
The time difference between two different time zones is measured in nanoseconds, and this result does not compensate for differences in calendar days or systems of time. This means that if two adjacent midnight dates cross the DST change, the difference between them could be less than 24 hours. The time difference in the code above would be 7415 days and 23 hours in Local Australian time, or 7415 days in full day.
Other resources
For the expression of span of time, please refer to Duration. For the expression of time interval, please refer to Stopwatch. DateTime class does not provide relevant functions of internationalization. For internationalization, you can use intL packages.
constant
April → const int 4 August → const int 8 daysPerWeek → const int 7 December → const int 12 February → const int 2 Friday → Const int 5 January → const int 1 July → const int 6 March → const int 3 May → const int 5 Monday → const int 1 monthsPerYear → const int 12 November → const int 11 October → const int 10 Saturday → const int 6 September → const int 9 Sunday → const int 7 Thursday → const int 4 Tuesday → const int 2 Wednesday → const int 3
Pure manual translation, thanks for your support!
reference
English Document address