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
Maintainers
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 Datesconst 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 agoAdding 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-24Calendar 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/2025Locale Support
const todayMoment = new W2Moment(new Date("2025-03-25"));
console.log(todayMoment.locale()); // Output: enTest 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!
