Advanced Usage

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.

Basic Usage

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:

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:

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:

Essential? No. Useful? Yeah.

A new way to use Instant...

You might notice an arrow below. That will take you to the newest feature of Instant: an brand-new Stopwatch class!

Last updated

Was this helpful?