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

@codedav/datetime-helper

v1.2.0

Published

A Node.js package for formatting dates and times.

Downloads

1

Readme

Date Time Helper

datetime-helper is a versatile Node.js package for formatting dates and times in various formats. It supports both CommonJS and ES Modules, making it easy to integrate into any Node.js project.

Installation

npm i @codedav/datetime-helper

Usage

CommonJS

For CommonJS, require the module and use the provided functions:

const {
  formatDateTime,
  formatToYearMonthDay,
  formatToDayMonthYear,
  formatToLongDate,
  formatToFullDate,
  formatToTime,
  addDaysToDate,
  subDaysFromDate,
  addMonthsToDate,
  subMonthsFromDate,
  addYearsToDate,
  subYearsFromDate,
  addHoursToDate,
  subHoursFromDate,
  addMinutesToDate,
  subMinutesFromDate,
  addSecondsToDate,
  subSecondsFromDate,
  formatLocale,
  convertToUTC
} = require("@codedav/datetime-helper");

const date = new Date();

console.log("Original Date:", date);
console.log("Formatted Date (yyyy-MM-dd):", formatToYearMonthDay(date));
console.log("Formatted Date (yyyy-MM-dd):", formatDateTime(date, "yyyy-MM-dd"));
console.log("Formatted Date (dd/MM/yyyy):", formatToDayMonthYear(date));
console.log("Formatted Date (dd/MM/yyyy):", formatDateTime(date, "dd/MM/yyyy"));
console.log("Formatted Date (MMMM do, yyyy):", formatToLongDate(date));
console.log("Formatted Date (EEEE, MMMM do, yyyy):", formatToFullDate(date));
console.log("Formatted Time (HH:mm:ss):", formatToTime(date));
console.log("Date plus 5 days:", addDaysToDate(date, 5));
console.log("Date minus 5 days:", subDaysFromDate(date, 5));
console.log("Date plus 2 months:", addMonthsToDate(date, 2));
console.log("Date minus 2 months:", subMonthsFromDate(date, 2));
console.log("Date plus 1 year:", addYearsToDate(date, 1));
console.log("Date minus 1 year:", subYearsFromDate(date, 1));
console.log("Date plus 3 hours:", addHoursToDate(date, 3));
console.log("Date minus 3 hours:", subHoursFromDate(date, 3));
console.log("Date plus 30 minutes:", addMinutesToDate(date, 30));
console.log("Date minus 30 minutes:", subMinutesFromDate(date, 30));
console.log("Date plus 45 seconds:", addSecondsToDate(date, 45));
console.log("Date minus 45 seconds:", subSecondsFromDate(date, 45));
console.log("Date in UTC (America/New_York):", convertToUTC(date, 'America/New_York'));
console.log("Formatted Date (French Locale):", formatLocale(date, 'fr'));

ES Module

For ES Modules, import the module and use the provided functions:

import {
  formatDateTime,
  formatToYearMonthDay,
  formatToDayMonthYear,
  formatToLongDate,
  formatToFullDate,
  formatToTime,
  addDaysToDate,
  subDaysFromDate,
  addMonthsToDate,
  subMonthsFromDate,
  addYearsToDate,
  subYearsFromDate,
  addHoursToDate,
  subHoursFromDate,
  addMinutesToDate,
  subMinutesFromDate,
  addSecondsToDate,
  subSecondsFromDate,
  formatLocale,
  convertToUTC
} from "@codedav/datetime-helper";

const date = new Date();

console.log("Original Date:", date);
console.log("Formatted Date (yyyy-MM-dd):", formatToYearMonthDay(date));
console.log("Formatted Date (yyyy-MM-dd):", formatDateTime(date, "yyyy-MM-dd"));
console.log("Formatted Date (dd/MM/yyyy):", formatToDayMonthYear(date));
console.log("Formatted Date (dd/MM/yyyy):", formatDateTime(date, "dd/MM/yyyy"));
console.log("Formatted Date (MMMM do, yyyy):", formatToLongDate(date));
console.log("Formatted Date (EEEE, MMMM do, yyyy):", formatToFullDate(date));
console.log("Formatted Time (HH:mm:ss):", formatToTime(date));
console.log("Date plus 5 days:", addDaysToDate(date, 5));
console.log("Date minus 5 days:", subDaysFromDate(date, 5));
console.log("Date plus 2 months:", addMonthsToDate(date, 2));
console.log("Date minus 2 months:", subMonthsFromDate(date, 2));
console.log("Date plus 1 year:", addYearsToDate(date, 1));
console.log("Date minus 1 year:", subYearsFromDate(date, 1));
console.log("Date plus 3 hours:", addHoursToDate(date, 3));
console.log("Date minus 3 hours:", subHoursFromDate(date, 3));
console.log("Date plus 30 minutes:", addMinutesToDate(date, 30));
console.log("Date minus 30 minutes:", subMinutesFromDate(date, 30));
console.log("Date plus 45 seconds:", addSecondsToDate(date, 45));
console.log("Date minus 45 seconds:", subSecondsFromDate(date, 45));
console.log("Date in UTC (America/New_York):", convertToUTC(date, 'America/New_York'));
console.log("Formatted Date (French Locale):", formatLocale(date, 'fr'));

Functions

Formatting Functions

  • formatDateTime(date, formatString): Formats a date and time according to the specified format string.
  • formatToYearMonthDay(date): Formats a date to yyyy-MM-dd.
  • formatToDayMonthYear(date): Formats a date to dd/MM/yyyy.
  • formatToLongDate(date): Formats a date to a long date format (e.g., MMMM do, yyyy).
  • formatToFullDate(date): Formats a date to a full date format (e.g., EEEE, MMMM do, yyyy).
  • formatToTime(date): Formats a time to HH:mm:ss.
  • formatLocale(date, localeCode): Formats a date using the specified locale. The localeCode parameter allows you to specify different language and regional formats. For example:
    • 'fr' for French, which will format the date in a way that is culturally appropriate for French speakers.
    • 'en' for English, which will format the date in a way that is culturally appropriate for English speakers.
    • 'es' for Spanish, which will format the date in a way that is culturally appropriate for Spanish speakers. This function uses the Intl.DateTimeFormat API to handle locale-specific date formatting.

Date Manipulation Functions

  • addDaysToDate(date, days): Adds days to a date.
  • subDaysFromDate(date, days): Subtracts days from a date.
  • addMonthsToDate(date, months): Adds months to a date.
  • subMonthsFromDate(date, months): Subtracts months from a date.
  • addYearsToDate(date, years): Adds years to a date.
  • subYearsFromDate(date, years): Subtracts years from a date.
  • addHoursToDate(date, hours): Adds hours to a date.
  • subHoursFromDate(date, hours): Subtracts hours from a date.
  • addMinutesToDate(date, minutes): Adds minutes to a date.
  • subMinutesFromDate(date, minutes): Subtracts minutes from a date.
  • addSecondsToDate(date, seconds): Adds seconds to a date.
  • subSecondsFromDate(date, seconds): Subtracts seconds from a date.
  • convertToUTC(date, timeZone): Converts a date to UTC based on a specific time zone.

License

This project is licensed under the ISC License.