Date and Time
Dates and times are created using the d() function, which returns a date object supporting method chaining for extraction, manipulation, comparison, and formatting.
Creating Dates
d
Creates a date object from a string. Supports natural language shortcuts and standard date formats.
d('now');
d('yesterday');
d('tomorrow');
d('2025-06-01');
d('2025-06-01 12:00');
d('2025-06-01T21:48:30Z');Extracting Components
All extraction methods are called on a date object using method chaining.
| Method | Description | Example | Result |
|---|---|---|---|
.year() |
Returns the year | d('2025-06-01').year() |
2025 |
.month() |
Returns the month (1–12) | d('2025-06-01').month() |
6 |
.day() |
Returns the day of the month | d('2025-06-15').day() |
15 |
.hour() |
Returns the hour (0–23) | d('2025-06-01 14:30').hour() |
14 |
.minute() |
Returns the minute (0–59) | d('2025-06-01 14:30').minute() |
30 |
.second() |
Returns the second (0–59) | d('2025-06-01 14:30:45').second() |
45 |
.weekday() |
Returns the day of the week (0=Sun) | d('2025-06-01').weekday() |
0 |
.dayOfYear() |
Returns the day of the year | d('2025-06-01').dayOfYear() |
152 |
.quarter() |
Returns the quarter (1–4) | d('2025-06-01').quarter() |
2 |
.timestamp() |
Returns the Unix timestamp (seconds) | d('2025-06-01').timestamp() |
1748736000 |
Date Arithmetic
.add
Adds a specified amount to a date. Returns a new date object.
d('2025-06-01').add(7, 'day') // 2025-06-08
d('2025-06-01').add(1, 'month') // 2025-07-01
d('2025-06-01').add(1, 'year') // 2026-06-01.sub
Subtracts a specified amount from a date. Returns a new date object.
d('2025-06-01').sub(1, 'day') // 2025-05-31
d('2025-06-01').sub(3, 'month') // 2025-03-01.diff
Calculates the difference between two dates in the specified unit.
d('2025-06-10').diff(d('2025-06-01'), 'day') // 9
d('2025-12-01').diff(d('2025-06-01'), 'month') // 6Boundary Methods
.startOf
Returns the start of a given time unit.
d('2025-06-15 14:30').startOf('day') // 2025-06-15 00:00:00
d('2025-06-15').startOf('month') // 2025-06-01 00:00:00.endOf
Returns the end of a given time unit.
d('2025-06-15 14:30').endOf('day') // 2025-06-15 23:59:59
d('2025-06-15').endOf('month') // 2025-06-30 23:59:59Comparison Methods
| Method | Description | Example | Result |
|---|---|---|---|
.isBefore() |
Returns true if the date is before another | d('2025-06-01').isBefore(d('2025-06-10')) |
true |
.isAfter() |
Returns true if the date is after another | d('2025-06-10').isAfter(d('2025-06-01')) |
true |
.isSame() |
Returns true if both dates are equal | d('2025-06-01').isSame(d('2025-06-01')) |
true |
.isToday() |
Returns true if the date is today | d('now').isToday() |
true |
.isYesterday() |
Returns true if the date is yesterday | d('yesterday').isYesterday() |
true |
.isTomorrow() |
Returns true if the date is tomorrow | d('tomorrow').isTomorrow() |
true |
Validation Methods
| Method | Description | Example | Result |
|---|---|---|---|
.isValid() |
Returns true if the date is valid | d('2025-06-01').isValid() |
true |
.isLeapYear() |
Returns true if the year is a leap year | d('2024-01-01').isLeapYear() |
true |
Formatting and Timezone
.format
Formats a date into a string using a format pattern.
d('2025-06-01').format('YYYY-MM-DD') // "2025-06-01"
d('2025-06-01 14:30').format('DD/MM/YYYY HH:mm') // "01/06/2025 14:30".tz
Converts a date to a specific timezone.
d('2025-06-01T12:00:00Z').tz('Asia/Singapore')
d('now').tz('America/New_York').set
Sets a specific component of the date.
d('2025-06-01').set('month', 12) // 2025-12-01
d('2025-06-01').set('day', 15) // 2025-06-15Duration
duration
Accepts a duration string and returns the total number of seconds. Supports combining multiple units.
duration('1h'); // 3600
duration('30m'); // 1800
duration('1h 30m'); // 5400
duration('1d 6h'); // 108000