1.0.1 • Published 2 years ago
eg-datetime-helper v1.0.1
eg-datetime-helper
A lightweight JavaScript library to simplify date and time handling.
Link to NPM package
https://www.npmjs.com/package/eg-datetime-helper
Installation
You can install eg-datetime-helper using npm:
npm install eg-datetime-helperUsage
Import the D`` class fromeg-datetime-helper and create an instance to easily work with dates and times.
import { D } from 'eg-datetime-helper';
const d = new D(2023, 7, 21, 15, 30, 0);
console.log(d.year); // 2023
console.log(d.month); // August
console.log(d.date); // 21
console.log(d.format()); // "2023 August 21"
console.log(d.when()); // "2 days from now"Features
- Get various date and time components such as year, month, day, hours, minutes, and seconds.
- Format dates using customizable masks (e.g., "Y-M-D h:I:S" or "y/m/d").
- Get human-readable time difference with the when() method (e.g., "2 days from now").
API
Class: D
The D class class provides various methods and properties to work with dates and times.
Constructor Create a new instance of the D class:
const d = new D(year, month, day, hours, minutes, seconds);year(optional): Year value (default: current year).month(optional): Month value (0-based index, default: current month).day(optional): Day value (default: current day).hours(optional): Hours value (default: 0).minutes(optional): Minutes value (default: 0).seconds(optional): Seconds value (default: 0).
Properties
year: Full year (4 digits).yr: Short year (2 digits).month: Full month name (e.g., "January").mon: Short month name (e.g., "Jan").day: Full day name (e.g., "Monday").dy: Short day name (e.g., "Mon").date: Day of the month (1-31).hours: Hour value (0-23).mins: Minute value (0-59).secs: Second value (0-59).
Methods
- format(mask): Format the date using the provided mask. Available mask characters:
Y: Full year (4 digits).y: Short year (2 digits).M: Full month name.m: Short month name.D: Zero-padded day of the month.d: Day of the month.#: Suffixed day of the month (e.g., "1st", "2nd").H: Zero-padded hour (24-hour format).h: Hour (24-hour format).I: Zero-padded minute.i: Minute.S: Zero-padded second.s: Second.
when(): Get a human-readable time difference between the current date and the instance date. Returns values like "3 days from now" or "1 year 2 months ago".