Chronos is a tiny, immutable, typed date manipulation library which does not bloat your JS bundle,
but does everything you need.
Features
- Immutable & pure function per file. Every function does not have side effects, nor mutates the params.
If you use a function, only this function is added to your bundle, not the whole lib.
- ESM & CommonJS. Works in Node.js and browser. Setup the target browsers for transpiling ES6 by your own in your bundler.
- TypeScript. Every function is typed and the typings are bundled with the package.
- Native API. Uses
Date
and Intl
under the hood. - Russian locale only. And it has docs in Russian.
Rationale
When we started to develop our projects, we picked the most popular date library that existed back then.
We had used only two methods it provided, but got all the bundled ones, with all the possible locales.
OK, we set up our bundler configs to remove those locales, but still that library was the biggest that we used, but the profit wasn't so huge.
We decided to look around for the date library that we need, but all of them had a lot of features that we didn't want to use.
So we replaced it with small and lightweight functions that just worked.
Step by step those functions have evolved into Chronos—simple yet useful date manipulation library that works in every our project.
Table of Contents
Getting Started
Add the package into deps:
npm install --save @funboxteam/chronos
Import functions:
import { addYears } from '@funboxteam/chronos';
Types
The library exports several types that may be used elsewhere, but the most important that they used inside the lib
to guarantee type safety.
ChronosDate
declare type ChronosDate = Date | number | string;
Every function that accepts date as a first param expects to get an instance of Date
, or a timestamp as number or string.
The timestamp may be present as seconds or milliseconds (e.g. 1596803254000
and 1596803254
is the same value).
Duration
declare type Duration = {
days: number;
hours: number;
minutes: number;
seconds: number;
};
Type describing return value of functions that work with time intervals.
Functions
Every function is immutable and those which accept Date
instances and return Date
instance always return
new Date
instance and do not mutate the passed one.
(value: ChronosDate, quantity: number) => Date;
Params
value
, date value;quantity
, number of units to add.
Example
addDays(new Date('2020-01-01T00:00:00.000Z'), 1);
addYears(1577836800, 1);
addMonths(new Date(2020, 0, 1), 1);
addMonths(new Date(2020, 0, 31), 1);
addCalendarMonths(new Date(2020, 0, 1), 1);
addCalendarMonths(new Date(2020, 0, 31), 1);
(value: ChronosDate, quantity: number) => Date;
Params
value
, date value;quantity
, number of units to subtract.
Example
subtractDays(new Date('2020-01-01T00:00:00.000Z'), 1);
subtractYears(1577836800, 1);
subtractMonths(new Date(2020, 0, 1), 1);
subtractMonths(new Date(2020, 2, 31), 1);
subtractCalendarMonths(new Date(2020, 0, 1), 1);
subtractCalendarMonths(new Date(2020, 2, 31), 1);
(value: ChronosDate, format: string) => string;
Params
value
, date value;format
, desired format.
Available format tokens
Type | Token | Value |
---|
Second | ss | 00, 01, 02, ..., 57, 58, 59 |
Minute | mm | 00, 01, 02, ..., 57, 58, 59 |
Hour | HH | 00, 01, 02, ..., 21, 22, 23 |
Day of Week | dddd | понедельник, вторник, ..., суббота, воскресенье |
Day of Month | DD | 01, 02, 03, ..., 29, 30, 31 |
| D | 1, 2, 3, ..., 29, 30, 31 |
Month | MMMM | январь, февраль, ..., ноябрь, декабрь |
| MMM | янв, фев, ..., ноя, дек |
| MM | 01, 02, 03, ..., 10, 11, 12 |
Year | YYYY | Full year, e.g.: 1885, 1955, 1985, 2015 |
| YY | 00, 01, 02, ..., 97, 98, 99 |
UTC time offset | ZZ | -1200, -1100, ..., +1300, +1400 |
| Z | -12:00, -11:00, ..., +13:00, +14:00 |
Example
formatDate(new Date(2020, 0, 1), 'YYYY-MM-DDTHH:mm:ssZ');
formatDate(1577836800, 'HH:mm:ss');
Important notes
Only Russian locale is supported for now!
(value: string, valueFormat: string, format: string) => string;
Params
value
, time string;valueFormat
, template describing value
format;format
, desired format.
Available format tokens
Type | Token | Value |
---|
Second | ss | 00, 01, 02, ..., 57, 58, 59 |
Minute | mm | 00, 01, 02, ..., 57, 58, 59 |
Hour | HH | 00, 01, 02, ..., 21, 22, 23 |
| H | 0, 1, 2, ..., 21, 22, 23 |
Example
formatTimeString('22:00', 'HH:mm', 'HH:mm:ss');
(value: ChronosDate) => number;
Params
Example
getDay(new Date(2020, 0, 1));
getYear(1577836800);
(value: ChronosDate, format?: string) => string;
Params
value
, date value;format
, format of the returned string. 'long'
(by default) or 'short'
.
Example
getWeekdayName(new Date(2020, 11, 30));
getWeekdayName(new Date(2020, 11, 30), 'short');
getMonthName(new Date(2020, 0, 1));
getMonthName(new Date(2020, 0, 1), 'short');
(seconds: number) => Duration;
Params
seconds
, interval value in seconds.
Example
getDuration(1000000);
(firstValue: ChronosDate, secondValue: ChronosDate) => boolean;
Params
firstValue
, date value;secondValue
, date value.
Example
isSameYear(1577750400, 1577836800);
(firstValue: ChronosDate, secondValue: ChronosDate) => number;
Params
firstValue
, date value;secondValue
, date value.
Example
getDiffInDays(1577750400, 1577836800);
(value: ChronosDate, diff?: number) => Date;
Params
value
, date value;diff
, number of units to add to the result date. 0
by default.
Example
getStartOfDay(1577836800);
getStartOfDay(1577836800, 1);
getStartOfDay(1577836800, -1);
(value: ChronosDate, diff?: number) => Date;
Params
value
, date value;diff
, number of units to add to the result date. 0
by default.
Example
getEndOfDay(1577836800);
getEndOfDay(1577836800, 1);
getEndOfDay(1577836800, -1);
(value: ChronosDate) => string;
Params
Example
getRelativeDate(1577081613);
getRelativeDate(new Date());
(value: ChronosDate) => number;
Params
Example
getUtcOffset(new Date(2020, 0, 1));
(date?: Date) => number;
Params
date
, Date instance. new Date()
by default.
Example
getUnixTimestamp();
getUnixTimestamp(new Date(2020, 0, 1));
() => string;
Example
getTimezoneName();
Important notes
In case of lack of Intl API support returns nearest to the user timezone
which has integer hours offset.
(value: string, format: string) => boolean;
Params
value
, time string;format
, format string that should be used for validation.
Example
isTimeValid('22:30', 'HH:mm');
(value: string, format: string) => Date;
Params
value
, date string;format
, format string that should be used for parsing.
Available format tokens
Type | Token | Recognized values |
---|
Day of Month | DD | 01, 02, 03, ..., 29, 30, 31 |
| D | 1, 2, 3, ..., 29, 30, 31 |
Month | MM | 01, 02, 03, ..., 10, 11, 12 |
Year | YYYY | Full year, e.g.: 1885, 1955, 1985, 2015 |
| YY | 00, 01, 02, ..., 97, 98, 99 |
Example
parseDate('2000-01-21', 'YYYY-MM-DD');
parseDate('2020-01-01T00:00:00+03:00');
Important notes
If format
is not passed it tries to parse value
using native
Date.parse
. It should support ISO 8601 and RFC 2822. Other formats
are not recommended to parse without explicit format
set.
Credits
Project's pictures were made by Igor Garybaldi.
![Sponsored by FunBox](https://funbox.ru/badges/sponsored_by_funbox_grayscale.svg)