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 🙏

© 2026 – Pkg Stats / Ryan Hefner

w2moment

v1.0.5

Published

The W2Moment library is a lightweight, custom-built utility for managing dates and times with features inspired by Moment.js.. It provides a wide range of functionality for formatting, parsing, manipulating, and displaying dates and times.

Downloads

15

Readme

W2Moment: A Custom Date and Time Management Library

Overview

The W2Moment library is a lightweight, custom-built utility for managing dates and times with features inspired by Moment.js.. It provides a wide range of functionality for formatting, parsing, manipulating, and displaying dates and times.

Features

Format dates and times using custom patterns (e.g., YYYY-MM-DD, MMMM Do YYYY).

Parse dates from strings with specific formats (e.g., YYYYMMDD).

Manipulate dates by adding or subtracting units of time (days, hours, minutes, seconds).

Display relative time (e.g., 13 years ago, 5 hours ago).

Implement calendar-based display (e.g., Today at 11:22 PM, Last Saturday at 11:22 PM).

Locale support for customizing months and weekdays.

Installation

Simply include the W2Moment class in your JavaScript project as follows: npm i w2moment

import W2Moment from "./src/w2moment.js";
Usage Examples
Formatting Dates
const moment = new W2Moment("20250325", "YYYYMMDD");
console.log(moment.format("YYYY")); // Output: 2025
console.log(moment.format("MMMM")); // Output: March
console.log(moment.format("L"));    // Output: 03/25/2025
console.log(moment.format("LL"));   // Output: March 25, 2025
Relative Time (fromNow)
const pastMoment = new W2Moment("20111031", "YYYYMMDD");
console.log(pastMoment.fromNow(new Date("2025-03-25"))); // Output: 13 years ago

const futureMoment = new W2Moment("20301031", "YYYYMMDD");
console.log(futureMoment.fromNow(new Date("2025-03-25"))); // Output: 5 years ago

Adding and Subtracting Time

const moment2 = new W2Moment(new Date("2025-03-25"));
console.log(moment2.add(1, "days").format("YYYY-MM-DD")); // Output: 2025-03-26
console.log(moment2.subtract(2, "days").format("YYYY-MM-DD")); // Output: 2025-03-24

Calendar Display

const testMoment = new W2Moment();

console.log(testMoment.subtract(10, "days").calendar()); // Output: 03/16/2025
console.log(testMoment.subtract(6, "days").calendar());  // Output: Last Wednesday at 11:22 PM
console.log(testMoment.subtract(3, "days").calendar());  // Output: Last Saturday at 11:22 PM
console.log(testMoment.subtract(1, "days").calendar());  // Output: Yesterday at 11:22 PM
console.log(testMoment.calendar());                     // Output: Today at 11:22 PM
console.log(testMoment.add(1, "days").calendar());       // Output: Tomorrow at 11:22 PM
console.log(testMoment.add(3, "days").calendar());       // Output: Friday at 11:22 PM
console.log(testMoment.add(10, "days").calendar());      // Output: 04/04/2025

Locale Support

const todayMoment = new W2Moment(new Date("2025-03-25"));
console.log(todayMoment.locale()); // Output: en

Test Cases To validate the functionality of W2Moment, you can use the provided custom test framework:

const expect = (actual) => {
  return {
    toBe(expected) {
      if (actual === expected) {
        console.log("Test Passed");
      } else {
        console.error(`Test Failed: Expected ${expected}, but got ${actual}`);
      }
    },
    toEqual(expected) {
      if (JSON.stringify(actual) === JSON.stringify(expected)) {
        console.log("Test Passed");
      } else {
        console.error(
          `Test Failed: Expected ${JSON.stringify(expected)}, but got ${JSON.stringify(actual)}`
        );
      }
    },
  };
};

Sample Tests

const moment1 = new W2Moment(new Date("2025-03-25 21:55:35"));
expect(moment1.format("LT")).toBe("09:55 PM");
expect(moment1.format("LTS")).toBe("09:55:35 PM");
expect(moment1.format("LLLL")).toBe("Tuesday, March 25, 2025 09:55 PM");

API Reference Constructor

new W2Moment(date = new Date(), format = null)

date: A JavaScript Date object or a date string.

format: Optional format string for parsing (e.g., YYYYMMDD).

Methods format(formatStr): Formats the date using the specified pattern.

fromNow(referenceDate): Displays relative time between the current date and the referenceDate.

add(amount, unit): Adds a specified amount of time (in days, hours, etc.).

subtract(amount, unit): Subtracts a specified amount of time (in days, hours, etc.).

startOf(unit): Sets the time to the beginning of the specified unit (day, hour).

endOf(unit): Sets the time to the end of the specified unit (day, hour).

calendar(referenceDate): Returns a calendar-style string representation of the date.

locale(locale): Sets the locale for month and weekday formatting.

License Feel free to use, modify, and distribute W2Moment as needed for your projects. Contributions are welcome!