🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

utils-datetime

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

utils-datetime

Easily handle and work with dates and times.

Source
npmnpm
Version
0.9.1
Version published
Weekly downloads
17
325%
Maintainers
1
Weekly downloads
 
Created
Source

utils-datetime

Easily handle and work with dates and times in your project. This library provides useful functions for working with dates and times in an easy and flexible way.

Installation

To install utils-datetime, simply run the following command in your terminal:

npm install utils-datetime

Import

import  { 
    getCurrentDate, 
    timeTransform, 
    dateTimeFormat 
}  from 'utils-datetime'

Available Functions

  • getCurrentDate = (timezone: string = 'UTC'): Gets the current date in the specified time zone.
const now = getCurrentDate('America/Asuncion')
console.log(now)
// 2023-10-23T22:14:49.876Z
  • timeTransform = (date: Date, timeDelta: ITimeDelta): Performs addition or subtraction operations on a date according to the provided options.
interface ITimeDelta {
    year?: number, 
    month?: number, 
    day?: number, 
    hour?: number, 
    minute?: number, 
    second?: number 
}
const options = {
  year: -1,
  day: 1,
  hour: 1,
  minute: -3,
  second: 120,
  month: -1
}
const now = getCurrentDate('America/Asuncion')
const transform = timeTransform(now, options)
console.log(transform)
// 2022-09-25T00:26:06.078Z
  • dateTimeFormat = (date: Date, template: string, language: string = 'en-EN'): Formats the date according to the specified template and language.
const now = getCurrentDate('America/Asuncion')
const template = "Today is %DAY%, %MONTH% %DD%rd of the year %YYYY%, the time is %H%:%MI% %A%"
const format = dateTimeFormat(now, template, 'es-EN')
console.log(format)
// Today is Monday, October 23rd of the year 2023, the time is 10:20 PM.
const now = getCurrentDate('America/Asuncion')
const template = "Today is %DAY%, %MONTH% %DD%rd of the year %YYYY%, the time is %HH%:%MI%:%SS% with %MS% milliseconds"
const format = dateTimeFormat(now, template, 'es-EN')
console.log(format)
// Today is Monday, October 23rd of the year 2023, the time is 22:20:20 with 345 milliseconds.
const now = getCurrentDate('America/Asuncion')
const template = "%DD%/%MM%/%YYYY% %HH%:%MI%"
const format = dateTimeFormat(now, template)
console.log(format)
// 23/10/2023 22:20
// %HH% 23h format
const now = getCurrentDate('America/Asuncion')
const template = "%DD%/%MM%/%YYYY% %H%:%MI% %A%"
const format = dateTimeFormat(now, template)
console.log(format)
// 23/10/2023 10:20 PM
// %H% 12h format

Keywords

date

FAQs

Package last updated on 25 Oct 2023

Did you know?

Socket

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