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

date-fusion

v1.0.6

Published

A comprehensive collection of utility functions for manipulating dates and times in JavaScript. Includes features for formatting, date arithmetic, and validity checks.

Downloads

395

Readme

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.