1.1.1 • Published 1 year ago

@invilite/date v1.1.1

Weekly downloads
-
License
GNU General Publi...
Repository
github
Last release
1 year ago

@invilite/date

Codacy Badge Maintainability Test Coverage Known Vulnerabilities Version License

Unit tests

Extends native Date object by adding new methods.

Highlights

  • No overriding built-in methods
  • No dependencies
  • Focus on high performance
  • TypeScript type definitions included

Install

This is a Node.js module available through the npm registry.

Using npm:

$ npm install @invilite/date

Using bower:

$ bower install @invilite/date

Using yarn:

$ yarn add @invilite/date

Usage

import "@invilite/date";

const date = new Date();

// Outputs: 2022-04-08 14:30:00.0
console.log(date.format("YY-MM-D HH:mm:ss.S"));

Methods

format()

Return the formatted date string in the given format, optionally converts to different timezone.

Syntax

format(format: string, locales?: string | string[], timezoneName?: string): string;

Example

const date = new Date();
const formattedDate = date.format("YY-MM-D HH:mm:ss.S");
const date = new Date("2022-04-18 15:30:00 +01:00");

// Convert time to Tokyo timezone
const formattedTime = date.format("YY-MM-D HH:mm:ss.S", "en-US", "Asia/Tokyo");

Accepted patterns:

PatternDescriptionExample
YA two digit representation of a year22
YYA full numeric representation of a year2022
MNumeric representation of a month, without leading zeros3
MMNumeric representation of a month, with leading zeros03
BFull month nameJanuary
bShort month nameJan
DDay of the month without leading zeros9
DDDay of the month, 2 digits with leading zeros09
wNumeric representation of the day of the week05
WWeek of the year28
H24-hour format of an hour without leading zeros17
HH24-hour format of an hour with leading zeros17
h12-hour format of an hour without leading zeros9
hh12-hour format of an hour with leading zeros09
mMinutes without leading zeros5
mmMinutes with leading zeros05
sSeconds without leading zeros0
ssSeconds with leading zeros00
SMilliseconds without leading zeros34
SSMilliseconds with leading zeros034
aLowercase Ante meridiem and Post meridiemam
AUppercase Ante meridiem and Post meridiemAM
zTimezone offset in minutes-120
ZTimezone offset in standard format+02:00
XUnit timestamp (in seconds)1659306131

toUnixTimestamp()

Get the seconds timestamp of the given date.

Syntax

toUnixTimestamp(): number;

Example

const date = new Date();
const timestamp = date.toUnixTimestamp();

addSeconds()

Add the specified number of seconds to the given date. Returns current object with the seconds added.

Syntax

addSeconds(seconds: number): Date;

Example

const date = new Date();
date.addSeconds(600);

diffFrom()

Get number of seconds, that represents time difference between two dates.

Syntax

diffFrom(date: Date | string = new Date()): number;

Example

const date = new Date("2022-04-18 15:50:40");
const futureDate = new Date("2022-04-18 14:30:00");

console.log(date.diffFrom(futureDate));
// Outputs: 4840

textDiffFrom()

Get formatted string (in English), that represents time difference between two dates.

Syntax

textDiffFrom(date: Date): string;

Example

const date = new Date("2022-04-18 15:50:40");
const futureDate = new Date("2022-04-18 14:30:00");

console.log(date.textDiffFrom(futureDate));
// Outputs: '1 hour, 20 minutes, 40 seconds'

Browser support

TBD

License

Library is licensed under a GNU General Public License v3.0

1.1.1

1 year ago

1.1.0

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.0

2 years ago