Skip to content

Repository files navigation

Now

A minimalist DateTime library for validating, parsing, manipulating and formatting times.

Installation

This will add a like this to you packages pubspec.yaml file:

dependencies:
now: latest

Or you can install it from the command line:

dart pub add now

Sponsors

Now is an MIT licensed open source project with its ongoing development made possible entirely by the support of these awesome backers. If you'd like to join them, please consider sponsoring Odroe development.

sponsors

Getting Started

Create a DateTime

/// Create a new DateTime from local time zone.final current =now(); // Same as DateTime.now()final utc = now.utc(); // Same as DateTime.now().toUtc()

Format a DateTime

final pattern ='YYYY-MM-DD HH:mm:ss.SSS';
final formated =now().format(pattern); // Same as DateTime.now().format(pattern)print(formated); // 2021-08-01 12:00:00.000

Convert a DateTime

Obtained a UTC safely originally from a different time zone.

final utc = date.isUtc ? date : date.toUtc();

Now you can convert it to a UTC time zone.

final utc = date.utc; // Same as date.isUtc ? date : date.toUtc();

DateTime addition and subtraction

final date =now();
/// Add 1 day.final tomorrow = date +1.day; // Same as date.add(Duration(days: 1));/// Subtract 1 day.final yesterday = date -1.day; // Same as date.subtract(Duration(days: 1));

DateTime comparison

  • Compare DateTime with DateTime less than or equal.

    final date1 =now();
    final date2 = date1 +1.day;
    print(date1 <= date2); // trueprint(date1.isBeforeOrEqual(date2)); // true
  • Compare DateTime with DateTime greater than or equal.

    final date1 =now();
    final date2 = date1 +1.day;
    print(date2 >= date1); // trueprint(date2.isAfterOrEqual(date1)); // true
  • Compare DateTime with DateTime less than.

    final date1 =now();
    final date2 = date1 +1.day;
    print(date1 < date2); // trueprint(date1.isBefore(date2)); // true
  • Compare DateTime with DateTime greater than.

    final date1 =now();
    final date2 = date1 +1.day;
    print(date2 > date1); // trueprint(date2.isAfter(date1)); // true

Duration extensions

Weeks in the duration

finalDuration duration =Duration(days:7);
print(duration.inWeeks); // 1

Convert a int to a Duration

UnitSame asDescriptionExample
microsecondsDuration(microseconds: <int>)Microseconds in the duration1.microsecond
millisecondsDuration(milliseconds: <int>)Milliseconds in the duration1.millisecond
secondsDuration(seconds: <int>)Seconds in the duration1.second
minutesDuration(minutes: <int>)Minutes in the duration1.minute
hoursDuration(hours: <int>)Hours in the duration1.hour
daysDuration(days: <int>)Days in the duration1.day
weeksDuration(days: <int> * 7)Weeks in the duration1.week

Format specifiers

By default we register all the built-in formatters.

specifierDescriptionExample
AAM/PM Upper casenow().format('A') -> AM
aam/pm Lower casenow().format('a') -> am
DDay of month, 1-31now().format('D') -> 1
DDDay of month, 01-31now().format('DD') -> 01
dDay of week, 1-7now().format('d') -> 1
ddDay of week min namenow().format('dd') -> Mo
dddDay of week short namenow().format('ddd') -> Mon
ddddDay of week full namenow().format('dddd') -> Monday
HHour, 0-23now().format('H') -> 0
HHHour, 00-23now().format('HH') -> 00
hHour, 1-12now().format('h') -> 1
hhHour, 01-12now().format('hh') -> 01
SSSMilliseconds, 000-999now().format('SSS') -> 000
mMinute, 0-59now().format('m') -> 0
mmMinute, 00-59now().format('mm') -> 00
MMonth, 1-12now().format('M') -> 1
MMMonth, 01-12now().format('MM') -> 01
MMMMonth abbreviated namenow().format('MMM') -> Jan
MMMMMonth full namenow().format('MMMM') -> January
QQuarter, 1-4now().format('Q') -> 1
sSecond, 0-59now().format('s') -> 0
ssSecond, 00-59now().format('ss') -> 00
YYYear, 00-99now().format('YY') -> 21
YYYYYear, 0000-9999now().format('YYYY') -> 2023
ZTimezonenow().format('Z') -> +01:00
ZZTimezonenow().format('ZZ') -> +0100

Escape specifiers

Occasionally, single-character specifiers exist for formatters. Our formatting mode hopes that this string will not be processed by the formatter, but will output the characters we defined as it is.

  • \ Prefix, a character after the \ symbol ensures output as is.

    now().format('HH:mm A ss'); // 00:00 AM 00// This is not what we expect, since the second s needs to be output as-is.now().format(r'HH:mm A s\s'); // 00:00 AM 0s// Ornow().format('HH:mm A s\\s'); // 00:00 AM 0s

DateTime formatter

Sometimes the built-in formatter cannot meet all your needs, you can register a DateTime formatter via now.register:

finalDateTimeFormatter formatter = ...
now.register(formatter);

You may already have a DateTime formatter, but its specifier is not what you expect:

finalDateTimeFormatter formatter = ...
now.registerWith('Custom specifier', formatter);

Custom formatter

If you wish to customize the DateTime formatter, you need to implement the DateTimeFormatter interface imported from package:now/formatter.dart:

import'package:now/formatter.dart';
classMyFormatterimplementsDateTimeFormatter {
constMyFormatter();
/// Matched the [specifier] and return the [DateTime] format result.@overrideStringformat(DateTime dateTime) {
// TODO: format the [dateTime] and return the result.return'My format result';
}
@overrideStringget specifier =>'My specifier';
}

Then we register and use it:

import'package:now/now.dart';
main() {
now.register(constMyFormatter());
print(now().format('YYYY - My specifier')); // Output: 2023 - My format result
}

Function-based formatter

If you don't want to implement the DateTimeFormatter interface, you can also use the function-based formatter:

import'package:now/now.dart';
main() {
now.registerWithFn('My specifier', (dateTime) {
return'My format result';
});
print(now().format('YYYY - My specifier')); // Output: 2023 - My format result
}

Stopwatch

We provide a helper method for Stopwatch that can be directly created and started:

final stopwatch = now.stopwatch(); // Same as Stopwatch()..start()

Destroy formatters

MethodDescriptionExample
now.destroy()Destroys a registered with a test function.now.destroy((f) => f.specifier == 'A')
now.destroyAll()Destroys all registered formatters.now.destroyAll()
now.destroyWith()Destroys a registered formatter.now.destroyWith('A')
now.destroyDefault()Destroys all built-in formatters.now.destroyDefault()

Contributing

We welcome contributions! Please read our contributing guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to Prisma.

Thank you to all the people who already contributed to Odroe!

Contributors

Code of Conduct

This project has adopted the Contributor Covenant Code of Conduct. For more information see the Code of Conduct FAQ or contact hello@odroe.com with any additional questions or comments.

Stay in touch

About

⏱️ A minimalist DateTime library for validating, parsing, manipulating and formatting times.

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages