Package Exports
- @utilify/date
- @utilify/date/dist/index.cjs
- @utilify/date/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@utilify/date) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Date Utilities
The @utilify/date package offers essential utilities for working with dates and times in JavaScript and TypeScript. These functions are designed to be intuitive, efficient, and type-safe.
Installation
To install the Date package, use one of the following commands:
npm install @utilify/dateyarn add @utilify/datepnpm add @utilify/dateImport the functions into your project:
import { formatDate, isLeapYear } from '@utilify/date';
const formatted = formatDate(new Date(), 'YYYY-MM-DD');
const leapYear = isLeapYear(2024);
console.log(formatted, leapYear);const { formatDate, isLeapYear } = require('@utilify/date');
const formatted = formatDate(new Date(), 'YYYY-MM-DD');
const leapYear = isLeapYear(2024);
console.log(formatted, leapYear);Overview
adjustDate
function adjustDate(date: Date, amount: number, unit: TimeUnit): DateAdjusts a date by adding or subtracting a time interval.
adjustTimezone
function adjustTimezone(date: Date, timezoneOffset: number): DateConverts a date to a different timezone.
convertTimeUnit
function convertTimeUnit(time: number, from: TimeUnit, to: TimeUnit): numberConverts time units, such as milliseconds to minutes.
convertDateTo
function convertDateTo(date: Date, format: DateFormat): string | undefinedConverts a date to another format or representation.
formatDate
function formatDate(date: Date, format: DateFormat = "DMY", separator: string = "/"): string | undefinedFormats a date into a user-friendly string.
formatDuration
function formatDuration(ms: number, format: string = "hh:mm:ss", autoHour: boolean = true): stringFormats a duration of time into a readable string.
formatTime
function formatTime(date: Date, format: string = "hh:mm:ss"): string | undefinedFormats the time component of a date.
isLeapYear
function isLeapYear(year: number): booleanChecks if a year is a leap year.
isValidDate
function isValidDate(date: Date): booleanValidates whether an object or string represents a valid date.
isValidDateString
function isValidDateString(date: string): booleanValidates whether a string follows a recognized date format.
isWeekday
function isWeekday(date: Date): boolean | undefinedChecks if a date is a weekday (Monday to Friday).
isWeekend
function isWeekend(date: Date): boolean | undefinedChecks if a date is a weekend (Saturday or Sunday).
parseDate
function parseDate(date: string): Date | undefinedConverts a string or number to a Date object.