date-fusion
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

DATE FUSION

A comprehensive utility package for handling date and time operations in JavaScript. This package provides functions to format dates, manipulate date values, and validate date and time strings. It also includes localization features for displaying numbers, dates, and times in different languages.

Table of Contents

Installation

To install the package, use npm or yarn:

npm install date-fusion or yarn add date-fusion

Usage

Here's how to use the functions provided by this package:

import {formatDate,dateDifference,addDays,isValidDate,getCurrentTime} from 'date-fusion';

// Example: Formatting a date
const date = new Date();
const formattedDate = formatDate(date, 'YYYY-MM-DD', 'en');
console.log(formattedDate); // Outputs: 2024-10-29

// Example: Calculating date difference
const startDate = new Date('2024-10-01');
const endDate = new Date('2024-10-29');
const diffDays = dateDifference(startDate, endDate, 'days');
console.log(diffDays); // Outputs: 28

// Example: Adding days to a date
const newDate = addDays(date, 5);
console.log(formatDate(newDate, 'YYYY-MM-DD', 'en')); // Outputs: 2024-11-03

// Example: Validating a date string
const isValid = isValidDate('2024-10-29');
console.log(isValid); // Outputs: true

// Example: Getting the current date and time
const currentTime = getCurrentTime();
console.log(currentTime); // Outputs: Current date and time

API Reference

formatDate

Formats a date into a specified format.

Parameters:

date: Date: The date to format. format: string: The format string (e.g., 'YYYY-MM-DD', 'MM-DD-YYYY','DD-MM-YYYY', 'MM/DD/YYYY', 'DD/MM/YYYY', 'MMMM DD, YYYY', 'DD MMMM YYYY' ). lang: keyof typeof languageData: The language code for month names (e.g.'en' | 'es' | 'fr' | 'de' | 'zh' | 'ar' | 'hi' | 'pt' | 'ru' | 'ja' | 'ko' | 'it' | 'nl' | 'sv' | 'tr' | 'pl'). Returns: string

const date = new Date('2023-10-29');
const formattedDate = formatDate(date, 'MMMM DD, YYYY', 'en'); // "October 29, 2023"

dateDifference

Calculates the difference between two dates.

Parameters:

startDate: Date: The start date. endDate: Date: The end date. unit: string: The unit of measurement (e.g., 'days', 'months', 'years'). Returns: number

const start = new Date('2023-01-01');
const end = new Date('2023-10-29');
const daysDifference = dateDifference(start, end, 'days'); // 302

addDays

Adds a specified number of days to a date.

Parameters:

date: Date: The original date. days: number: The number of days to add. Returns: Date

const today = new Date();
const futureDate = addDays(today, 10); // Date 10 days from today

isValidDate

Checks if a date string is valid.

Parameters:

dateString: string: The date string to validate. Returns: boolean

subtractDays

Subtracts a specified number of days from a date.

Parameters:

date: Date: The original date. days: number: The number of days to subtract. Returns: Date

const today = new Date();
const pastDate = subtractDays(today, 5); // Date 5 days ago

getFirstDayOfMonth

Gets the first day of the month for a given date.

Parameters:

date: Date: The date. Returns: Date

const date = new Date('2023-10-29');
const firstDay = getFirstDayOfMonth(date); // October 1, 2023

getLastDayOfMonth

Gets the last day of the month for a given date.

Parameters:

date: Date: The date. Returns: Date

const date = new Date('2023-10-29');
const lastDay = getLastDayOfMonth(date); // October 31, 2023

isLeapYear

Checks if a year is a leap year.

Parameters:

year: number: The year to check. Returns: boolean

const isLeap = isLeapYear(2020); // true
const isLeap2 = isLeapYear(2021); // false

getDaysInMonth

Gets the number of days in a given month and year.

Parameters:

month: number: The month (0-11). year: number: The year. Returns: number

const daysInFebruary2023 = getDaysInMonth(1, 2023); // 28
const daysInFebruary2024 = getDaysInMonth(1, 2024); // 29

isSameDay

Checks if two dates are on the same day.

Parameters:

date1: Date: The first date. date2: Date: The second date. Returns: boolean

const date1 = new Date('2023-10-29');
const date2 = new Date('2023-10-29');
const isSame = isSameDay(date1, date2); // true

formatTime

Formats a time into a specified format.

Parameters:

date: Date: The date object containing the time to format. format: string: The format string (e.g., 'HH:mm', 'hh:mmA', or 'Hhr:Mmin'). lang: keyof typeof languageData: The language code for country language (e.g.'en' | 'es' | 'fr' | 'de' | 'zh' | 'ar' | 'hi' | 'pt' | 'ru' | 'ja' | 'ko' | 'it' | 'nl' | 'sv' | 'tr' | 'pl').

Returns: string

const date = new Date('2023-10-29T14:30:00');
const formattedTime = formatTime(date, 'hh:mm:ss A', 'en'); // "02:30:00 PM"

addHours

Adds a specified number of hours to a time.

Parameters:

date: Date: The original date. hours: number: The number of hours to add. Returns: Date

const now = new Date();
const newTime = addHours(now, 3); // Time 3 hours from now

subtractHours

Subtracts a specified number of hours from a time.

Parameters:

date: Date: The original date. hours: number: The number of hours to subtract. Returns: Date

const now = new Date();
const pastTime = subtractHours(now, 2); // Time 2 hours ago

addMinutes

Adds a specified number of minutes to a time.

Parameters:

date: Date: The original date. minutes: number: The number of minutes to add. Returns: Date

const now = new Date();
const newTime = addMinutes(now, 15); // Time 15 minutes from now

subtractMinutes

Subtracts a specified number of minutes from a time.

Parameters:

date: Date: The original date. minutes: number: The number of minutes to subtract. Returns: Date

const now = new Date();
const pastTime = subtractMinutes(now, 10); // Time 10 minutes ago

addSeconds

Adds a specified number of seconds to a time.

Parameters:

date: Date: The original date. seconds: number: The number of seconds to add. Returns: Date

const now = new Date();
const newTime = addSeconds(now, 30); // Time 30 seconds from now

subtractSeconds

Subtracts a specified number of seconds from a time.

Parameters:

date: Date: The original date. seconds: number: The number of seconds to subtract. Returns: Date

const now = new Date();
const pastTime = subtractSeconds(now, 20); // Time 20 seconds ago

isValidTime

Checks if a time string is valid.

Parameters:

timeString: string: The time string to validate (e.g., '14:30' or '2:30 PM'). Returns: boolean

const valid24 = isValidTime('14:30'); // true
const valid12 = isValidTime('2:30 PM'); // true
const invalid = isValidTime('25:00'); // false

getCurrentTime

Gets the current time.

Returns: Date

const currentTime = getCurrentTime(); // Current date and time

Contribution

Contributions are welcome! Please check out Saroj Gurung for more details.

License

This project is licensed under the MIT License.

Package Sidebar

Install

npm i date-fusion

Weekly Downloads

0

Version

1.0.6

License

ISC

Unpacked Size

54.9 kB

Total Files

6

Last publish

Collaborators

  • sarojgurung