
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
utils-datetime
Advanced tools
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.
To install utils-datetime
, simply run the following command in your terminal:
npm install utils-datetime
import {
getCurrentDate,
timeTransform,
dateTimeFormat,
dateDiff,
isLeapYear,
currentWeekNumber,
formatMilliseconds,
} from "utils-datetime";
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', offset: string = '+0')
: Returns the current date in the specified time zone, with optional time offset adjustment.const now = getCurrentDate("America/Asuncion");
console.log(now);
// 2023-10-23T22:14:49.876Z
const later = getCurrentDate("America/Asuncion", "1h");
console.log(later);
// 2023-10-23T23:14:49.876Z
Supported offset
formats:
Milliseconds: "+1000"
, "-2000"
Seconds: "30s"
, "-45s"
Minutes: "15m"
, "-10m"
Hours: "1h"
, "-3h"
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
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
type YearInterface = number | string;
const leapYear = isLeapYear(2024);
console.log(leapYear);
// true
const result = currentWeekNumber("2023-02-27");
console.log(result);
// 9
const result = formatMilliseconds(3661000);
console.log(result);
// 01:01:01
FAQs
Easily handle and work with dates and times.
The npm package utils-datetime receives a total of 30 weekly downloads. As such, utils-datetime popularity was classified as not popular.
We found that utils-datetime demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.