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

nativx

v1.1.3

Published

A powerful and easy-to-use JavaScript library for date and time manipulation. nativx provides a comprehensive set of functions for parsing, validating, manipulating, and formatting dates.

Downloads

47

Readme

nativx

nativx is a lightweight and versatile JavaScript library for seamless date and time operations using native JavaScript methods. It provides utility functions to get the current date and time, validate date instances, add or subtract time, and format dates in customized formats, making date manipulation both easy and efficient.

Installation

You can install nativx via npm:

npm install nativx

Module Support

  • CommonJS
  • ES Modules (ESM)

Usage

Importing the Module

import { currentDateTime, add, subtract, formatter, isAfter, isBefore, isSameDay } from 'nativx';

Functions

currentDateTime()

Returns the current date and time.

const now = currentDateTime();
console.log(now); // Outputs the current date and time

validateDateInstance(input)

Validates whether the input is a valid Date instance. Throws a TypeError if the date is invalid.

try {
    validateDateInstance(new Date());
    console.log("Valid date");
} catch (error) {
    console.error(error.message);
}

add(date, { hours, minutes, seconds, days, years, months })

Adds the specified amount of time to the given date.

  • date (Date): The date to which time will be added. Defaults to the current date and time.
  • hours, minutes, seconds, days, years, months (number): The amount of time to add.
const newDate = add(new Date(), { days: 5, hours: 2 });
console.log(newDate); // Outputs the new date with the added time

subtract(date, { hours, minutes, seconds, days, years, months })

Subtracts the specified amount of time from the given date.

  • date (Date): The date from which time will be subtracted. Defaults to the current date and time.
  • hours, minutes, seconds, days, years, months (number): The amount of time to subtract.
const newDate = subtract(new Date(), { days: 1, hours: 3 });
console.log(newDate); // Outputs the new date with the subtracted time

formatter(date, format, isUTC = false)

Formats the given date according to the specified format string.

  • date (Date): The date to format.
  • format (string): The format string, using placeholders like 'YYYY', 'MM', 'DD', 'HH', 'mm', 'ss'.
  • isUTC (boolean): Whether to use UTC time. Defaults to false.
const formattedDate = formatter(new Date(), 'YYYY-MM-DD HH:mm:ss');
console.log(formattedDate); // Outputs the formatted date string

const formattedUTCDate = formatter(new Date(), 'YYYY-MM-DD HH:mm:ss', true);
console.log(formattedUTCDate); // Outputs the formatted UTC date string

isAfter(date1, date2)

Checks if date1 is after date2.

const date1 = new Date('2024-07-28T10:15:30');
const date2 = new Date('2024-07-30');

console.log(isAfter(date1, date2)); // Outputs: false

isBefore(date1, date2)

Checks if date1 is before date2.

console.log(isBefore(date1, date2)); // Outputs: true

isSameDay(date1, date2)

Checks if date1 is on the same calendar day as date2.

console.log(isSameDay(date1, date2)); // Outputs: false

Example

import { currentDateTime, add, subtract, formatter, isAfter, isBefore, isSameDay } from 'nativx';

// Get current date and time
const now = currentDateTime();
console.log('Current Date and Time:', now);

// Add time to current date
const futureDate = add(now, { days: 10, hours: 5 });
console.log('Future Date:', futureDate);

const futureDate2 = add(new Date('2023-01-01'), { days: 10 });
console.log('Future Date 2:', futureDate2);

const futureDate3 = add(new Date(), { seconds: 45 });
console.log('Future Date 3:', futureDate3);

const futureDate4 = add(new Date(), { days: 5, hours: 4, minutes: 30, seconds: 15 });
console.log('Future Date 4:', futureDate4);

const futureDate6 = add('2024-01-01', { days: 10 });
console.log('Future Date 6:', futureDate2);

const futureDate7 = add('2024-07-28T10:15:30Z', { seconds: 45 });
console.log('Future Date 7:', futureDate3);

const futureDate8 = add('2024-07-28T10:15:30Z', { days: 5, hours: 4, minutes: 30, seconds: 15 });
console.log('Future Date 8:', futureDate8);

const futureDate9 = add('2024-07-28T10:15:30Z', { years: 2, months: 3 });
console.log('Future Date 9:', futureDate9);


// Subtract time from current date
const pastDate = subtract(now, { days: 2, hours: 3 });
console.log('Past Date:', pastDate);

const pastDate2 = subtract(new Date(), { years: 2, months: 3 });
console.log('Past Date 2:', pastDate2);

const pastDate3 = subtract(new Date(), { seconds: 45 });
console.log('Past Date 3:', pastDate3);

const pastDate4 = subtract(new Date(), { days: 5, hours: 4, minutes: 30, seconds: 15 });
console.log('Past Date 4:', pastDate4);

const pastDate5 = subtract("2024-07-28", { days: 2, hours: 3 });
console.log('Past Date 5 :', pastDate5);

const pastDate6 = subtract("2024-07-28T10:15:30Z", { years: 2, months: 3 });
console.log('Past Date 6:', pastDate6);

const pastDate7 = subtract("2024-07-28T10:15:30Z", { seconds: 45 });
console.log('Past Date 7:', pastDate7);

const pastDate8 = subtract("2024-07-28T10:15:30Z", { days: 5, hours: 4, minutes: 30, seconds: 15 });
console.log('Past Date 8:', pastDate8);

// Format date
const formattedDate = formatter(now, 'YYYY-MM-DD HH:mm:ss');
console.log('Formatted Date:', formattedDate);

// Format Date with am/pm
const formattedDate1 = new Date('2023-07-28T15:45:00');
console.log(formatter(formattedDate1, 'YYYY-MM-DD hh:mm:ss a')); // Outputs: "2023-07-28 03:45:00 PM"

const formattedDate2 = new Date('2023-07-28T10:15:30Z');
console.log(formatter(formattedDate2, 'YYYY-MM-DD HH:mm:ss', true)); // Outputs: "2023-07-28 10:15:30"

// Format date in UTC
const formattedUTCDate = formatter(now, 'YYYY-MM-DD HH:mm:ss', true);
console.log('Formatted UTC Date:', formattedUTCDate);

// Comparison: isBefore, isAfter, isSameDay
const date1 = new Date('2024-07-28T10:15:30');
const date2 = new Date('2024-07-30');

console.log('Is date1 before date2?', isBefore(date1, date2)); // true
console.log('Is date2 after date1?', isAfter(date1, date2)); // false
console.log('Is date1 the same day as date2?', isSameDay(date1, date2)); // false

// Comparison involving the current date
const currentDate = new Date();
const futureDateComparison = add(currentDate, { days: 1 });
const pastDateComparison = subtract(currentDate, { days: 1 });

console.log('Is current date before future date?', isBefore(currentDate, futureDateComparison)); // true
console.log('Is past date after current date?', isAfter(pastDateComparison, currentDate)); // false
console.log('Is current date the same as current date?', isSameDay(currentDate, currentDate)); // true

// Calculate detailed age based on a specific birthdate
const ageDetails = calculateAge('1990-08-15');
console.log(`Age Details: ${ageDetails.years} years, ${ageDetails.months} months, ${ageDetails.days} days, ${ageDetails.hours} hours, ${ageDetails.seconds} seconds`);

// Calculate detailed age based on a birthdate and a reference date
const ageDetailsAtReferenceDate = calculateAge('1990-08-15', '2023-01-01');
console.log(`Age on 2023-01-01: ${ageDetailsAtReferenceDate.years} years, ${ageDetailsAtReferenceDate.months} months, ${ageDetailsAtReferenceDate.days} days, ${ageDetailsAtReferenceDate.hours} hours, ${ageDetailsAtReferenceDate.seconds} seconds`);

Related Articles:-

  1. Announcing nativx: Simplifying Date and Time Operations in JavaScript

  2. Working with Dates and Times in JavaScript using nativx

  3. Github Nativx Project Usecase

License

This project is licensed under the MIT License.

Upcoming Features

  1. Enhanced UTC functionality
  2. Age calculation utility