Using every one of Instant's features, and doing it well.
Picking Back Up
We now know (or, at least, assume that you know) the basics of Instant from Basic Usage. If not, this section assumes you have that knowledge, so go give it a read.
Moving on, there's a few different topics this section aims to cover:
Taking Differences between DateTimes
Inbuilt functions dealing with current local DateTime
Converting between timezones
Taking Differences
Taking differences between DateTimes is one of the most valuable things that we can do with Instant, and also one of the most practical.
Suppose we start with a DateTime:
DateTime first =curDateTimeByZone(zone:'PST');
Then, some unknown amount of time elapses. Of course, you could deal with Dart's clunky stopwatch to measure this period, but this way is much cleaner:
DateTime second =curDateTimeByZone(zone:'PST');var diff =diffInSecs(first, second);
This gives you a clean int value for seconds that transpired in between. From there, you can do what you want: convert it to a Duration, display the difference, do calculations. But the power is back in your hands.
The other major advantage is the amount of units you can get the difference in:
This again provides you with some great flexibility in use.
However, if you still want to use a Stopwatch-esque class, take a look at the Stopwatch tab on the left.
Functions Dealing in (Local) Current Time
For convenience, because local time is what we most often deal with, Instant offers a number of abstractions just dealing in the current local time. Most of these are self-explanatory. They also have analogues which take in DateTimes instead of using the local timezones. Both are below, side-by-side:
local.dart
//You also have formatCurTime, but it's the same ideaStringformatCurDate({String format ="MMDDYYYY", String divider ="/"})// Returns the current local date in words (ex. January 3, 2019)StringcurDateInWords()// Returns local current DateTime's millisecond as a # String formatted III (ex. '004').StringcurMillisecAsString()// Returns local current DateTime's second as a # String formatted SS (ex. '04').StringcurSecAsString() // Returns local current DateTime's minute as a # String formatted MM (ex. '04').StringcurMinAsString() // Returns local current DateTime's hour as a # String formatted HH (ex. '14').StringcurHrAsString()// Returns local current day as a # String formatted DD (ex. '04').StringcurDayAsString()// Returns local current month as a # String formatted MM (ex. '04').StringcurMonthAsString()// Returns local current year as a # String formatted YYYY (ex. '2019').StringcurYearAsString()/// Returns current day of the week as a text String (ex. "Monday").StringcurWeekdayAsString()// Returns current millisecond as an int (0...999).// NO GIVEN DATETIME ANALOGUE, use DateTime().millisecond insteadintcurMillisecAsInt()// Returns current second as an int (0...59).// NO GIVEN DATETIME ANALOGUE, use DateTime().second insteadintcurSecAsInt()// Returns current minute as an int (0...59).// NO GIVEN DATETIME ANALOGUE, use DateTime().minute insteadintcurMinAsInt()// Returns current hour as an int (0...23).// NO GIVEN DATETIME ANALOGUE, use DateTime().hour insteadintcurHrAsInt()// Returns current day as an int (1...31).// NO GIVEN DATETIME ANALOGUE, use DateTime().day insteadintcurDayAsInt()// Returns current month as an int (1...12).// NO GIVEN DATETIME ANALOGUE, use DateTime().month insteadintcurMonthAsInt()// Returns current year as an int (2019...Infinity).// NO GIVEN DATETIME ANALOGUE, use DateTime().year insteadintcurYearAsInt()
given.dart
//You also have formatTime, but it's the same ideaStringformatDate({@requiredDateTime date, String format ="MMDDYYYY", String divider ="/"})// Returns a date in words (ex. January 3, 2019)StringdateInWords({@requiredDateTime date})// Returns a DateTime's millisecond as a # String formatted III (ex. '004').StringmillisecAsString({@requiredDateTime time})// Returns a DateTime's second as a # String formatted SS (ex. '04').StringsecAsString({@requiredDateTime time}) // Returns a DateTime's minute as a # String formatted MM (ex. '04').StringminAsString({@requiredDateTime date}) // Returns a DateTime's hour as a # String formatted HH (ex. '14').StringhrAsString({@requiredDateTime date})// Returns given DateTime's day as a # String formatted DD (ex. '04').StringdayAsString({@requiredDateTime date})// Returns given DateTime's month as a # String formatted MM (ex. '04').StringmonthAsString({@requiredDateTime date})// Returns given DateTime's year as a # String formatted YYYY (ex. '2019').StringyearAsString({@requiredDateTime date})/// Returns given DateTime's day of the week as a text String (ex. "Monday").StringweekdayAsString({@requiredDateTime date})
Converting Between Timezones
This is by far the shortest section, but that might be expected. It's basically an extension on a method you (hopefully) should remember from Basic Usage.
I'm talking about DateTime conversion between timezones. We've already seen getting the current time in a timezone:
DateTime curInSFO =curDateTimeByZone(zone:"PDT");
But what if we later need that San Francisco DateTime to become a New York DateTime? Or a Paris DateTime? Or what if we have some historical data to make into a Moscow DateTime? Enter conversion: