Introduction

Time and date are objects that we often use in programs. However, the processing of time and date is not very useful because of different time zones. Just like in Java, multiple APIs have been modified and added for time and date. As a new language, dart, is there any difference?

Two very important classes of date and time in dart are DateTime and Duration.

Among them, DateTime represents the time, and Duration represents the time difference.

DateTime

Let's take a look at the use of DateTime first.

DateTime represents a point in time. Because the world clock has UTC and local time. Therefore, when using DataTime, these two clocks can also be used.

The easiest is to get the current time:

var now = DateTime.now();

If you want to create a time for a specified date, you can pass the year, month, and day to the DateTime constructor:

var now = DateTime(2021, 11, 20); 

Note that the date created above is a local date.

If you want to create UTC time, you can use the DateTime.utc method:

var utc = DateTime.utc(2021, 11, 20);

Another way to express time is unix time. Unix time refers to the number of seconds that have passed since January 1, 1970.

DateTime has two ways to express Unix time, they are:

  DateTime.fromMicrosecondsSinceEpoch(10000);
  DateTime.fromMillisecondsSinceEpoch(10000);

The difference between them is that one represents microseconds and the other represents milliseconds.

DateTime can also convert a string into a DateTime object:

var time= DateTime.parse('2002-02-27T14:00:00-0500');

In fact, DateTime.parse can accept multiple character types, as shown below:

 `"2012-02-27"`
`"2012-02-27 13:27:00"`
 `"2012-02-27 13:27:00.123456789z"`
`"2012-02-27 13:27:00,123456789z"`
`"20120227 13:27:00"`
`"20120227T132700"`
`"20120227"`
`"+20120227"`
 `"2012-02-27T14Z"`
`"2012-02-27T14+00:00"`

Duration

Duration represents the difference between two times.

Take a look at the constructor of Duration:

  const Duration(
      {int days = 0,
      int hours = 0,
      int minutes = 0,
      int seconds = 0,
      int milliseconds = 0,
      int microseconds = 0})
      : this._microseconds(microsecondsPerDay * days +
            microsecondsPerHour * hours +
            microsecondsPerMinute * minutes +
            microsecondsPerSecond * seconds +
            microsecondsPerMillisecond * milliseconds +
            microseconds);

You can see that Duration can represent the interval from days to microseconds, which is enough. How should it be used?

var time = DateTime.now();

// 添加一年
var nextYear = time.add(const Duration(days: 365));
assert(nextYear.year == 2022);

Similarly, we can also subtract Duration:

var time = DateTime.now();

//减少一年
var lastYear = time.subtract(const Duration(days: 365));
assert(lastYear.year == 2020);

Of course, you can also calculate the difference between two dates:

var duration = nextYear.difference(time);
assert(duration.inDays == 365);

Summarize

The above is the support for time and date in dart.

This article has been included in http://www.flydean.com/17-dart-date-time/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "programs, those things", know the technology, know you better!


flydean
890 声望435 粉丝

欢迎访问我的个人网站:www.flydean.com