A simple date class with methods to manipulate the date and format the date
npm install @timclark/sdateA simple and immutable date library for TypeScript/JavaScript.
sdate provides a straightforward and chainable API for common date manipulations, ensuring that you always work with predictable and immutable date objects.
``bash`
npm install @timclark/sdate
Import the sdate class and create a new instance.
`typescript
import sdate from '@timclark/sdate';
// Create a date object for today
const today = new sdate();
// Create a date object for a specific date
const specificDate = new sdate('2023-10-27');
// The sdate object is immutable. All manipulation methods return a new sdate instance.
const nextWeek = specificDate.addDays(7);
console.log(specificDate.toString()); // Output: 2023-10-27
console.log(nextWeek.toString()); // Output: 2023-11-03
`
Creates a new sdate instance.dateString
- If no argument is provided, it initializes with the current date.
- : An optional string in YYYY-MM-DD format or a Date object.
---
instance.---
$3
Adds a specified number of months to the date. Returns a new sdate instance.---
$3
Adds a specified number of years to the date. Returns a new sdate instance.---
$3
Calculates the difference in days between the sdate instance and another.---
$3
Formats the date as DD/MM/YYYY.---
$3
Checks if the sdate instance is equal to another sdate or Date object.---
$3
Returns the date as a string in YYYY-MM-DD format.---
$3
Returns the four-digit year.---
$3
Returns the last two digits of the year.---
$3
Returns the month (1-12).---
$3
Returns the month, padded with a leading zero if necessary (e.g., 01, 12).---
$3
Returns the day of the month (1-31).---
$3
Returns the day of the month, padded with a leading zero if necessary.---
$3
Returns the day of the week (0 for Sunday, 1 for Monday, etc.).---
$3
Returns an object containing the year, month, and day.---
$3
Returns an object containing the year, month, day, and day of the week.---
$3
Returns a new sdate instance for the start of the week (Monday).---
$3
Returns an array of sdate objects for the entire week (Monday to Sunday).---
$3
Checks if the sdate instance exists in an array of dates.---
$3
Checks if the date is in a specific month. Defaults to the current month if no arguments are provided.---
$3
Checks if the date is today.---
$3
Returns a new sdate instance for the first day of the month.---
$3
Returns a new sdate instance for the last day of the month.---
$3
Returns an array of sdate` objects for every day in the month.