pols-date
v1.7.3
Published
Class for managin date and time.
Downloads
518
Readme
Pols-Date
Class for managin date and time.
import { PDate } from 'pols-date'
const now = new PDateIt uses a Date object internally.
Constructor
Current date and time
const mydate = new PDatePass string parameter
const mydate1 = new PDate('2025-03-06') // Pass only date. Time is 00:00:00.000
const mydate2 = new PDate('2025-03-06 18:30:00') // Pass date and time
const mydate3 = new PDate('2025-03-06 18:30:00.000') // Pass date and time with millisecond
const mydate4 = new PDate('1') // Create a date with current year, current month, 1st, and current clock time
const mydate5 = new PDate('15') // Create a date with current year, current month, 15th, and current clock time
const mydate6 = new PDate('1503') // Create a date with current year, March, 15th, and current clock time
const mydate7 = new PDate('150325') // Create a date with 2025, March, 15th, and current clock timeUse Date object
const anotherdate = new Date
const mydate = new PDate(anotherdate) //Copy another DateUse PDate object
const anotherdate = new PDate
const mydate = new PDate(anotherdate) //Copy another PDatePass values
const mydate = new PDate({
year: 2025, //optional
month: 3, //optional
day: 6, //optional
hour: 18, //optional
minute: 30, //optional
second: 0, //optional
millisecond: 0, //optional
})Properties
engine: Get/Set the Date internal object.year: Get/Set the year.month: Get/Set the month.day: Get/Set the day.weekDay: Get the week day (0for Sunday through6for Saturday).hour: Get/Set the hour.minute: Get/Set the minute.second: Get/Set the second.millisecond: Get/Set the millisecond.timestamp: Get the timestamp value.isInvalidDate: Returnstrueif the date is invalid.
Methods
setFrom(value): this: Update the date usingvalue, that must be the same type for the constructor.addYear(value): this: Increase/decrease year.addMonth(value): this: Increase/decrease month.addDay(value): this: Increase/decrease day.addHour(value): this: Increase/decrease hour.addMinute(value): this: Increase/decrease minute.addSecond(value): this: Increase/decrease second.addMillisecond(value): this: Increase/decrease millisecond.clearClockTime(): this: Set00:00:00.000for time.setClockTime(value): this: Set the clock time.
mydate.setClockTime('15:20:30')
/* or */
mydate.setClockTime('15:20:30.000')daysDifference(value): number: Get the days difference between this object and another PDate.
const date1 = new PDate('2025-01-01')
const date2 = new PDate('2025-01-10')
console.log(date1.daysDifference(date2)) // -9
console.log(date2.daysDifference(date1)) // 9minutesDifference(value): number: Get the minutes difference between this object and another PDate.
const date1 = new PDate('2025-01-01 00:01:00')
const date2 = new PDate('2025-01-01 00:58:30')
console.log(date1.minutesDifference(date2)) // -57
console.log(date2.minutesDifference(date1)) // 57clone(): PDate instance: Get a copy of this object.toDate(): Date instance: Get a copy of this engine object.toString(mask?, language?): string: Get string representation.maskis a string value, it recognizes those wildcards:@y: Year@m: Month (1for january through12for december).@mm: Month with leading zero (01for january through12for december).@mmm: Shortname for Month (Janfor january throughDecfor december).@mmmm: Name for Month (Januaryfor january throughDecemberfor december).@d: Day (1through31).@dd: Day with leading zero (01through31).@ddd: Short weekday name (Sunfor sunday throughSatfor saturday).@dddd: Weekday name (Sundayfor sunday throughSaturdayfor saturday).@w: Week number on the year.@h: 24-Hour format (0through23).@hh: 24-Hour format with leading zero (00through23).@o: 12-Hour format (1through12).@oo: 12-Hour format with leading zero (01through12).@i: Minute (0through59).@ii: Minute with leading zero (00through59).@s: Second one digit (0through59).@ss: Second with leading zero (00through59).@l: Millisecond one digit (0through999).@ll: Millisecond with leading zero (00through999).@lll: Millisecond with leading zero (000through999).@e: 12-Hour format symbol (aandp).@ee: 12-Hour format symbol (amandpm).@eee: 12-Hour format symbol (a.m.andp.m.).@E: 12-Hour format symbol (AandP).@EE: 12-Hour format symbol (AMandPM).@EEE: 12-Hour format symbol (A.M.andP.M.).
language: Set the language for the month names and weekday names. If it doesn't use, the static propertyPDate.languageprevails.
const date = new PDate('2025-03-06 18:30:45.567')
console.log(date.toString()) // 2025-03-06 18:30:45.567
console.log(date.toString('@y-@mm-@dd @hh:@ii:@ss.@lll')) // 2025-03-06 18:30:45.567
console.log(date.toString('@y-@m-@d')) // 2025-3-6
console.log(date.toString('@dd/@mm/@y')) // 06/03/2025toJSON(): string: Like totoString().
Static Class Properties
language: Set the language for alltoStringmethods for all instancesPDate.
Date manipulation
Change some part of the date:
const date = new PDate('2025-03-06 18:30:45.567')
date.month = 11 // Set month to 11: 2025-11-06 18:30:45.567
date.month += 2 // Add two months: 2026-01-06 18:30:45.567
date.addMonth(2) // Add two months: 2026-03-06 18:30:45.567; same to date.month += 2
date.addMonth(-3) // Substract three months: 2025-12-06 18:30:45.567; same to date.month -= 3
date.addMonth(1).addDay(3) // Add one month and three days: 2025-01-09 18:30:45.567Invalid date
const date = new PDate('invalid value')
console.log(date.isInvalidDate) // true
date.addMonth(1) // Throws Error: The date is invalidThe following methods will throw an error if called on an invalida date:
addYear(value)addMonth(value)addDay(value)addHour(value)addMinute(value)addSecond(value)addMillisecond(value)
The following properties will throw an error if modified on an invalida date:
yearmonthdayhourminutesecondmillisecond
The following methods and properties will return undefined when called on an invalid date:
console.log(date.year) // Returns undefined
console.log(date.month) // Returns undefined
console.log(date.day) // Returns undefined
console.log(date.weekDay) // Returns undefined
console.log(date.hour) // Returns undefined
console.log(date.minute) // Returns undefined
console.log(date.second) // Returns undefined
console.log(date.millisecond) // Returns undefined
console.log(date.toDate()) // Returns undefined
console.log(date.clone()) // Returns undefined
console.log(date.toString()) // Returns ''