

GitHub - jogboms/time.dart: ⏰ Type-safe DateTime and Duration calculations, powe...
source link: https://github.com/jogboms/time.dart
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Time
With shiny extensions, if you have ever written something like this, then look no further:
final DateTime fourHoursFromNow = DateTime.now() + Duration(hours: 4);
Installation
dependencies: time: "^1.3.0"
Import
import 'package:time/time.dart';
Usage
final Duration tenMinutes = 10.minutes; final Duration oneHourThirtyMinutes = 1.5.hours; final DateTime afterTenMinutes = DateTime.now() + 10.minutes; final Duration tenMinutesAndSome = 10.minutes + 15.seconds; final int tenMinutesInSeconds = 10.minutes.inSeconds; final DateTime tenMinutesFromNow = 10.minutes.fromNow;
You can perform all basic arithmetic operations on Duration
as you always have been:
final Duration interval = 10.minutes + 15.seconds - 3.minutes + 2.hours; final Duration doubled = interval * 2;
You can also use these operations on DateTime
:
final DateTime oneHourAfter = DateTime() + 1.hours;
Duration
is easily convertible as it always has been:
final int twoMinutesInSeconds = 2.minutes.inSeconds;
You can also convert Duration
to DateTime
, if needed:
final DateTime timeInFuture = 5.minutes.fromNow; final DateTime timeInPast = 5.minutes.ago;
Iterate through a DateTime
range:
final DateTime start = DateTime(2019, 12, 2); final DateTime end = start + 1.weeks; final DateTime tuesday = start.to(end).firstWhere((date) => date.weekday == DateTime.tuesday);
You can also delay code execution:
void doSomething() async { await 5.seconds.delay; // Do the other things }
You can also use the popular copyWith
:
final initial = DateTime(2019, 2, 4, 24, 50, 45, 1, 1); final expected = initial.copyWith( year: 2021, month: 10, day: 28, hour: 12, minute: 45, second: 10, millisecond: 0, microsecond: 12, );
Bugs/Requests
If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on Github and I'll look into it. Pull request are also welcome.
Inspiration
License
MIT License
Recommend
-
139
This micro-library is made for you if: You have ever written something like this: let interval: TimeInterval = 10 * 60 To represent 10 minutes. Usage Showcase import...
-
20
大家好,马上就要过年了,大家是不是都快回家了。 前段时间才哥主要在忙于工作和游戏,以及准备肥家事宜(比如预约核酸)。
-
15
一、 DateTime 的成员属性 DateTime 是和日期、时间相关的类,自身比较简单,本文从源码的角度看看内部实现,能不能寻找到一些有价值的知识,如果没有的话,就当是巩固基础啦。 当前sdk : Flutter 1.22.6 --
-
19
Tips for working with date, time, and datetime values in DS2 » SAS博客列表 As a programmer, you might be accustomed to how SAS® stores date, time, and datetime values as 8-byte floating-point numeric values. A SAS dat...
-
12
DateTime add and subtract & daylight saving time 2021-04-022021-04-02 by marc The combination of daylight saving times and D...
-
8
datetime — Basic date and time types
-
4
【Golang】理解Golang中的time.Duration taoshihan1991 · 大约18小...
-
4
Introduction A common Variable Pay request is to perform calculations based upon employees’ salaries as of a certain date, usually the end of the year. When integrating Variable Pay with Employee Central, this seemingly innocuous request...
-
7
Long-duration energy storage: The time is now by Courtney Stenson,
-
5
September 20, 2022 /
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK