npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

utils-datetime

v0.9.5

Published

Easily handle and work with dates and times.

Downloads

52

Readme

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,
    dateDiff,
    isLeapYear,
    currentWeekNumber,
    formatMilliseconds 
}  from 'utils-datetime'

Available Functions

  • dateDiff = (dates: DateInterface, timePeriod: TimePeriod): is a function that calculates the time difference between two dates in milliseconds, seconds, minutes, hours, days, weeks, months, or years, depending on the specified time period.
interface DateInterface {
    start: Date | string
    end: Date | string
}

type TimePeriod = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years'
const dates = {
    start: new Date('2022-01-01'),
    end: new Date('2023-01-01')
}

const result = dateDiff(dates, 'days')
console.log(result)
// 365
  • 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
  • const isLeapYear = (yearInterface: YearInterface): checks if a given year (after 1582) is a leap year according to the Gregorian calendar.
type YearInterface = number | string
const leapYear = isLeapYear(2024)
console.log(leapYear)
// true
  • const currentWeekNumber = (date: Date | string): takes a Date or date string, calculates and returns the ISO week number, assuming a Monday start.
const result = currentWeekNumber('2023-02-27')
console.log(result)
// 9
  • const formatMilliseconds = (milliseconds: number | string, includeMS: boolean = false): takes a time value in milliseconds (as a string or number) and returns it as HH:MM:SS. If includeMS is true, it adds milliseconds as HH:MM:SS.MS
const result = formatMilliseconds(3661000)
console.log(result)
// 01:01:01