The original link

I have been studying Dart source code recently, and I will write an article about Date to sort out and share it.

Manipulating dates in Dart is done through the DateTime class.

Because DateTime is built into Dart, no import is required.

parsing

  • DateTime
  • DateTime.parse
  • DateTime.tryParse
  • DateTime.utc

A date can be resolved using the datetime.parse static method

A case in point:

String str = 'the 2020-02-20 22:48:18';
DateTime date = DateTime.parse(str);

print(date);  / / the 22:48:18 2020-02-20. 000
Copy the code

The parse argument takes a string that can be parsed in the following format:

/ * * *"The 2012-02-27 13:27:00"
  * "The 2012-02-27 13:27:00. 123456789 z"
  * "The 2012-02-27 13:27:00, 123456789 z"
  * "20120227 13:27:00"
  * "20120227T132700"
  * "20120227"
  * "+ 20120227"
  * "2012-02-27T14Z"
  * "2012-02-27T14+00:00"
  * "-123450101 00:00:00 Z"
  * "2002-02-27T14:00:00-0500"* /Copy the code

The parse method also has a static counterpart to tryParse, which is essentially the syntactic sugar of parse

TryParse does not throw an exception if the received argument cannot be parsed, but returns NULL

TryParse’s internal implementation:

try {
  return parse(formattedString);
} on FormatException {
  return null;
}
Copy the code

It can also be parsed through a DateTime instance, and can accept 8 parameters, the first is mandatory, and the rest are optional, as follows:

/** * DateTime(int year, * [int month = 1, * int day = 1, * int hour = 0, * int minute = 0, * int second = 0, * int millisecond = 0, * int microsecond = 0]) */
DateTime date = DateTime(2020);

// 22 February 2020, 22 hours, 22 minutes, 22 seconds
DateTime date2 = DateTime(2020.2.22.22.22.22);
Copy the code

If you want to parse to UTC, you can call its static methods with the same arguments as DateTime

DateTime date1 = DateTime.utc(2020, 2, 22, 22, 22, 22);
Copy the code

The values

Get current time

DateTime n = DateTime.now();
print(n);   / / the 23:05:04 2020-02-20. 332418
Copy the code

Gets a timestamp in milliseconds

int t = DateTime.now().millisecondsSinceEpoch;
print(t);  / / 1582211214849
Copy the code

Gets a timestamp in microseconds

int t = DateTime.now().microsecondsSinceEpoch;
print(t);  / / 1582211566844552
Copy the code

Get the current time zone

String timeZone = DateTime.now().timeZoneName;
print(timeZone);
Copy the code

Get the day of the week

int w = DateTime.now().weekday;
print(w);  / / 4
Copy the code

Get the offset of the current time zone (Beijing time zone is 8 hours ahead of UTC)

Duration offset = DateTime.now().timeZoneOffset;
print(offset);    / / 8:00:00. 000000
Copy the code

Get the current year

int year = DateTime.now().year;
print(year);      / / 2020
Copy the code

Get the current month (1-12), which is a bit different from JS

int month = DateTime.now().month;
print(month);      / / 2
Copy the code

Get the day of the current month (1-31)

int day = DateTime.now().day;
print(day);      / / 21
Copy the code

Get the current hour (0-23)

int hour = DateTime.now().hour;
print(hour);      / / 8
Copy the code

Get the current minute (0-59)

int minute = DateTime.now().minute;
print(minute);      / / 18
Copy the code

Get the current second (0-59)

int second = DateTime.now().second;
print(second);      / / 18
Copy the code

Get the current millisecond (0-999)

int millisecond = DateTime.now().millisecond;
print(millisecond);      / / 249
Copy the code

Get current microseconds (0-999)

int microsecond = DateTime.now().microsecond;
print(microsecond);      / / 249
Copy the code

According to

  • toString
  • toIso8601String
  • toLocal
  • toUtc

ToString To convert a DateTime to time, use the toString method

String str = DateTime.now().toString();
print(str);     / / the 01:01:01 2020-02-22. 182096
Copy the code

ToIso8601String converts to ISO 8601

String str = DateTime.now().toIso8601String();
print(str);   / / T22:2020-02-22 12:30. 159729
Copy the code

ToLocal returns this DateTime value in the local time zone

DateTime date = DateTime.now().toLocal();
print(date);   / / the 22:27:57 2020-02-22. 641258
Copy the code

ToUtc returns this DateTime value in the UTC time zone

DateTime date = DateTime.now().toUtc();
print(date);   / / the 2020-02-22 03:27:09. 564188 z
Copy the code

operation

  • Add () changes the original DateTime by adding time
  • Subtract () changes the original DateTime by subtracting time

The add() argument accepts Duration, and the following example adds an hour instead of 0 hours

Duration oneHours = Duration(hours: 1);
String date = DateTime(2020).add(oneHours).toString();
print(date);    / / the 01:00:00 2020-01-01. 000
Copy the code

Subtract () is the reverse operation of add() and is used in the same way as add.

The query

  • isBefore
  • isAfter
  • isAtSameMomentAs
  • compareTo

IsBefore checks whether one DateTime precedes another DateTime

DateTime date1 = DateTime(2020);
DateTime date2 = DateTime(2019);

print(date1.isBefore(date2));   // false
Copy the code

IsAfter checks whether a DateTime comes after another DateTime

DateTime date1 = DateTime(2020);
DateTime date2 = DateTime(2019);

print(date1.isAfter(date2));   // true
Copy the code

IsAtSameMomentAs Returns true if this event occurs at the same time as other.

DateTime date1 = DateTime(2020);
DateTime date2 = DateTime(2019);

print(date1.isAtSameMomentAs(date2));   // false
Copy the code

CompareTo determines whether two datetimes are equal and returns 0 if they are equal otherwise -1

DateTime date1 = DateTime(2020);
DateTime date2 = DateTime(2020);

print(date1.compareTo(date2));   / / 0
Copy the code