Socket
Socket
Sign inDemoInstall

@invilite/date

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @invilite/date

Extends native JavaScript Date object by adding new methods.


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

@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

Keywords

FAQs

Last updated on 17 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc