Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- master
paths:
- 'docs/**'
- 'public/**'
- 'astro.config.mjs'
- '.github/workflows/deploy-docs.yml'
workflow_dispatch:
Expand Down
2 changes: 1 addition & 1 deletion docs/api/addYears.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const nyDate = new Date('2024-03-10T05:00:00Z'); // March 10, 2024 05:00 UTC (DS

// Add years in New York timezone
const futureNY = addYears(nyDate, 1, 'America/New_York');
console.log(futureNY); // March 10, 2025 04:00 UTC (EST, DST adjusted)
console.log(futureNY); // March 10, 2025 04:00 UTC (EDT, DST adjusted)

// UTC calculation for comparison
const futureUTC = addYears(nyDate, 1, 'UTC');
Expand Down
2 changes: 1 addition & 1 deletion docs/api/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ compile(formatString)

### Returns

`CompiledObject` - A compiled format object that can be reused with `format()` and `parse()`
`CompiledObject` - A compiled format object that can be reused with `format()`, `parse()`, `preparse()`, `isValid()`, and `transform()`

## Basic Usage

Expand Down
8 changes: 4 additions & 4 deletions docs/api/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ format(now, 'hh:mm A [GMT]Z');
| `ss` | Seconds | 05, 10 |
| `s` | Seconds without zero padding | 5, 10 |
| `SSS` | 3-digit milliseconds | 753, 022 |
| `SS` | 2-digit milliseconds | 75, 02 |
| `S` | 1-digit milliseconds | 7, 0 |
| `SS` | First 2 digits of milliseconds (truncated) | 75, 02 |
| `S` | First digit of milliseconds (truncated) | 7, 0 |

### AM/PM Tokens

Expand Down Expand Up @@ -173,8 +173,8 @@ const date = new Date();
format(date, 'YYYY-MM-DD HH:mm:ss [JST]', { timeZone: 'Asia/Tokyo' });
// => 2025-08-23 23:30:45 JST

format(date, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: 'America/New_York' });
// => 2025-08-23 09:30:45 EST
format(date, 'YYYY-MM-DD HH:mm:ss [EDT]', { timeZone: 'America/New_York' });
// => 2025-08-23 10:30:45 EDT

format(date, 'YYYY-MM-DD HH:mm:ss [UTC]', { timeZone: 'UTC' });
// => 2025-08-23 14:30:45 UTC
Expand Down
18 changes: 10 additions & 8 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ Welcome to the comprehensive API reference for date-and-time v4.x. This section

| Function | Description |
|--------------------------------------------------------|--------------------------------------------------|
| [`addYears()`](./add-functions#addyears) | Add/subtract years from dates |
| [`addMonths()`](./add-functions#addmonths) | Add/subtract months from dates |
| [`addDays()`](./add-functions#adddays) | Add/subtract days from dates |
| [`addHours()`](./add-functions#addhours) | Add/subtract hours from dates |
| [`addMinutes()`](./add-functions#addminutes) | Add/subtract minutes from dates |
| [`addSeconds()`](./add-functions#addseconds) | Add/subtract seconds from dates |
| [`addMilliseconds()`](./add-functions#addmilliseconds) | Add/subtract milliseconds from dates |
| [`addYears()`](./addyears) | Add/subtract years from dates |
| [`addMonths()`](./addmonths) | Add/subtract months from dates |
| [`addDays()`](./adddays) | Add/subtract days from dates |
| [`addHours()`](./addhours) | Add/subtract hours from dates |
| [`addMinutes()`](./addminutes) | Add/subtract minutes from dates |
| [`addSeconds()`](./addseconds) | Add/subtract seconds from dates |
| [`addMilliseconds()`](./addmilliseconds) | Add/subtract milliseconds from dates |
| [`subtract()`](./subtract) | Calculate time differences with Duration objects |

### Utility Functions
Expand Down Expand Up @@ -137,7 +137,9 @@ if (isNaN(result.getTime())) {
### Type Safety

```typescript
import { format, FormatterOptions } from 'date-and-time';
import { format } from 'date-and-time';

type FormatterOptions = NonNullable<Parameters<typeof format>[2]>;

// TypeScript will catch type errors
const options: FormatterOptions = {
Expand Down
2 changes: 2 additions & 0 deletions docs/api/isValid.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ isValid('2025-08-23', 'MM/DD/YYYY'); // => false (wrong format)
isValid('08/23/2025', 'YYYY-MM-DD'); // => false (wrong format)
```

A format string must contain at least one token: a format string made only of literal text, such as `'[today]'`, never validates. When the `timeZone` option names a timezone the JavaScript runtime does not recognize, `isValid()` returns `false` unless the input contains an offset, matching the `Invalid Date` that [`parse()`](./parse) returns.

## Validation with Options

### Locale Validation
Expand Down
53 changes: 44 additions & 9 deletions docs/api/parse.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ parse('2025-08-23 14:30:45', 'YYYY-MM-DD HH:mm:ss');
| `ss` | Seconds | 05, 10 |
| `s` | Seconds without zero padding | 5, 10 |
| `SSS` | 3-digit milliseconds | 753, 022 |
| `SS` | 2-digit milliseconds | 75, 02 |
| `S` | 1-digit milliseconds | 7, 0 |
| `SS` | Hundredths of a second (75 is 750 ms) | 75, 02 |
| `S` | Tenths of a second (7 is 700 ms) | 7, 0 |

### AM/PM Tokens

Expand Down Expand Up @@ -167,6 +167,8 @@ Interprets the parsed date in the specified timezone.

**Note**: If the input string contains a timezone offset (e.g., `Z` or `ZZ` tokens), that offset takes precedence and the `timeZone` option is ignored.

**Note**: A timezone name that the JavaScript runtime does not recognize makes `parse()` return `Invalid Date`, and `isValid()` return `false`, unless the input contains an offset.

```typescript
import { parse } from 'date-and-time';

Expand Down Expand Up @@ -202,11 +204,11 @@ import beng from 'date-and-time/numerals/beng';

// Arabic-Indic numerals
parse('٠٨/٠٧/٢٠٢٥', 'DD/MM/YYYY', { numeral: arab });
// => Fri Aug 08 2025 00:00:00 GMT-0700
// => Tue Jul 08 2025 00:00:00 GMT-0700

// Bengali numerals
parse('০৮/০৭/২০২৫', 'DD/MM/YYYY', { numeral: beng });
// => Fri Aug 08 2025 00:00:00 GMT-0700
// => Tue Jul 08 2025 00:00:00 GMT-0700
```

**Available numeral systems:**
Expand Down Expand Up @@ -257,6 +259,8 @@ parse('0:30 AM', 'h:mm A', { hour12: 'h11' });
// => Thu Jan 01 1970 00:30:00 GMT-0800
```

An hour outside the selected range fails: `h12` rejects `0`, and `h11` rejects `12`.

### hour24

**Type**: `"h23" | "h24"`
Expand All @@ -271,11 +275,13 @@ import { parse } from 'date-and-time';
parse('0:30', 'H:mm', { hour24: 'h23' });
// => Thu Jan 01 1970 00:30:00 GMT-0800

// h24 format - midnight is 24 (of previous day)
// h24 format - midnight is 24, read as 00 of the same date
parse('24:30', 'H:mm', { hour24: 'h24' });
// => Thu Jan 01 1970 00:30:00 GMT-0800
```

An hour outside the selected range fails: `h23` rejects `24`, and `h24` rejects `0`.

### ignoreCase

**Type**: `boolean`
Expand All @@ -295,6 +301,8 @@ parse('AUGUST 23, 2025', 'MMMM D, YYYY', { ignoreCase: true });
// => Sat Aug 23 2025 00:00:00 GMT-0700
```

`ignoreCase` compares names using the locale's own case rules. In Greek, where capital letters drop the accent, `ΑΥΓΟΥΣΤΟΣ` matches `Αύγουστος`. Accents that a language keeps in capitals must still match: with the French locale, `AOÛT` matches `août`, but `AOUT` does not.

### defaultDate

**Type**: `ParsedComponents`
Expand Down Expand Up @@ -473,6 +481,30 @@ parse('11:30:45 PM', 'h:mm:ss A');
// => Thu Jan 01 1970 23:30:45 GMT-0800
```

When the input contains a 24-hour value (`H` or `HH`), it is used and `h`, `hh`, and the meridiem are ignored. A meridiem without an hour reads as 0 (AM) or 12 (PM):

```typescript
parse('05 PM', 'HH A');
// => Thu Jan 01 1970 05:00:00 GMT-0800

parse('PM', 'A');
// => Thu Jan 01 1970 12:00:00 GMT-0800
```

### Fractional Seconds

`SSS` reads one to three digits as milliseconds. `SS` and `S` read a fraction of a second, so their digits are scaled: `SS` reads one or two digits as hundredths, and `S` reads exactly one digit as tenths.

```typescript
parse('12', 'SSS').getMilliseconds(); // => 12
parse('12', 'SS').getMilliseconds(); // => 120
parse('1', 'S').getMilliseconds(); // => 100
```

### Timezone Offset Range

`Z` reads an offset written as `+HHmm`, and `ZZ` reads one written as `+HH:mm`. The offset must be between `-15:56` and `+15:13`, the extremes found in the IANA timezone history; an offset outside this range fails.

## Advanced Usage

### Comments in Format Strings
Expand All @@ -481,8 +513,9 @@ Parts of the format string enclosed in square brackets are treated as literal te

```typescript
import { parse } from 'date-and-time';
import { parser as day_of_week } from 'date-and-time/plugins/day-of-week';

parse('Today is Saturday, August 23, 2025', '[Today is] dddd, MMMM D, YYYY');
parse('Today is Saturday, August 23, 2025', '[Today is] dddd, MMMM D, YYYY', { plugins: [day_of_week] });
// => Sat Aug 23 2025 00:00:00 GMT-0700

parse('2025-08-23T14:30:45Z', 'YYYY-MM-DD[T]HH:mm:ss[Z]');
Expand All @@ -498,7 +531,7 @@ parse('[2025-08-23 14:30:45]', '\\[YYYY-MM-DD HH:mm:ss\\]');

### Wildcard Parsing

Whitespace in the format string acts as a wildcard token, allowing you to skip corresponding parts of the input string. The character count must match between the format string and input string.
Each space in the format string is a wildcard that matches any one character of the input, so a format string with spaces also accepts other separators in those positions (`'YYYY-MM-DD HH:mm'` accepts `2025-08-23T14:30`). To skip part of the input, use one space per character to skip.

```typescript
import { parse } from 'date-and-time';
Expand All @@ -508,7 +541,7 @@ parse('2025/08/23 14:30:45', 'YYYY/MM/DD');
// => Invalid Date

// Use whitespace as wildcard (9 spaces to match ' 14:30:45')
parse('2025/08/23 14:30:45', 'YYYY/MM/DD ');
parse('2025/08/23 14:30:45', 'YYYY/MM/DD ');
// => Sat Aug 23 2025 00:00:00 GMT-0700
```

Expand Down Expand Up @@ -537,11 +570,13 @@ parse('2025/08', 'YYYY/MM/DD...');
```typescript
import { parse } from 'date-and-time';
import fr from 'date-and-time/locales/fr';
import { parser as day_of_week } from 'date-and-time/plugins/day-of-week';

// French with timezone
parse('samedi, 23 août 2025 à 14:30:45', 'dddd, D MMMM YYYY [à] HH:mm:ss', {
locale: fr,
timeZone: 'Europe/Paris'
timeZone: 'Europe/Paris',
plugins: [day_of_week]
});
// => Sat Aug 23 2025 14:30:45 GMT+0200
```
Expand Down
19 changes: 13 additions & 6 deletions docs/api/preparse.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ preparse(dateString, formatString[, options])

### Returns

`ParseResult` - Object containing parsed date components
`PreparseResult` - Object containing parsed date components

```typescript
interface ParseResult {
interface PreparseResult {
Y?: number // Year
M?: number // Month (1-12)
D?: number // Day
Expand All @@ -33,8 +33,6 @@ interface ParseResult {
m?: number // Minutes (0-59)
s?: number // Seconds (0-59)
S?: number // Milliseconds (0-999)
SS?: number // 2-digit milliseconds (0-99)
SSS?: number // 3-digit milliseconds (0-999)
Z?: number // Timezone offset in minutes
_index?: number // Current parsing position
_length?: number // Total length of input string
Expand All @@ -53,6 +51,9 @@ import { preparse } from 'date-and-time';
const result = preparse('2025-08-23 14:30:45', 'YYYY-MM-DD HH:mm:ss');
console.log(result);
// {
// _index: 19,
// _length: 19,
// _match: 6,
// Y: 2025,
// M: 8,
// D: 23,
Expand All @@ -68,13 +69,16 @@ console.log(result);
const result = preparse('2025-08-23 2:30:45 PM', 'YYYY-MM-DD h:mm:ss A');
console.log(result);
// {
// _index: 21,
// _length: 21,
// _match: 7,
// Y: 2025,
// M: 8,
// D: 23,
// A: 1, // PM
// h: 2,
// m: 30,
// s: 45
// s: 45,
// A: 1 // PM
// }
```

Expand All @@ -87,6 +91,9 @@ import ja from 'date-and-time/locales/ja';
const result = preparse('2025年8月23日', 'YYYY年M月D日', { locale: ja });
console.log(result);
// {
// _index: 10,
// _length: 10,
// _match: 3,
// Y: 2025,
// M: 8,
// D: 23
Expand Down
33 changes: 31 additions & 2 deletions docs/api/subtract.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ subtract(date1, date2)

### Returns

`Duration` - Rich duration object containing the time difference with properties for years, months, days, hours, minutes, seconds, and milliseconds
`Duration` - Object holding the time difference, which `toDays()`, `toHours()`, `toMinutes()`, `toSeconds()`, `toMilliseconds()`, `toMicroseconds()`, and `toNanoseconds()` express in the unit you need

## Basic Examples

Expand Down Expand Up @@ -124,6 +124,31 @@ console.log(duration.toMinutes().value); // 120
console.log(duration.toHours().format('H[h] m[m]')); // "2h 0m"
```

### Format Tokens

`format()` on a duration accepts these tokens. Repeating a token's letter zero-pads the value to that many digits (`HH` prints `01`). A token for a unit larger than the one you converted to is printed as is, because that unit is not part of the result.

| Token | Unit |
|-------|--------------|
| `D` | Days |
| `H` | Hours |
| `m` | Minutes |
| `s` | Seconds |
| `S` | Milliseconds |
| `f` | Microseconds |
| `F` | Nanoseconds |

```typescript
import { Duration } from 'date-and-time';

new Duration(90061001.123456).toDays().format('D[d] HH:mm:ss.SSS fff FFF');
// => "1d 01:01:01.001 123 456"

// toHours() has no days, so D is printed as is
new Duration(90061001).toHours().format('D H');
// => "D 25"
```

### Advanced Duration Formatting

```typescript
Expand Down Expand Up @@ -200,8 +225,12 @@ console.log(duration.toDays().value); // -0.09375 (negative but less than 1 da
const parts = duration.toDays().toParts();
console.log(parts); // { days: 0, hours: -2, minutes: -15, seconds: 0, ... }

// format() places a single negative sign at the beginning, potentially creating "-0"
// format() puts the negative sign on the largest unit only, which can print "-0"
console.log(duration.toDays().format('D[day], H:mm:ss')); // "-0day, 2:15:00"

// Without that unit's token, the sign is not printed at all
console.log(duration.toDays().format('H:mm:ss')); // "2:15:00"
console.log(duration.toHours().format('H:mm:ss')); // "-2:15:00"
```

### Sub-millisecond Precision
Expand Down
14 changes: 8 additions & 6 deletions docs/api/transform.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ console.log(time24); // '14:30:45'

```typescript
import es from 'date-and-time/locales/es';
import { parser as day_of_week } from 'date-and-time/plugins/day-of-week';

// Transform Spanish to English UTC
const localized = transform(
'viernes, 23 ago 2025 14:30:45 GMT+0200',
'sábado, 23 ago 2025 14:30:45 GMT+0200',
'dddd, DD MMM YYYY HH:mm:ss [GMT]Z',
'ddd, DD MMM YYYY HH:mm:ss [GMT]',
{ locale: es }
{ locale: es, plugins: [day_of_week] },
{ timeZone: 'UTC' }
);
console.log(localized); // 'Fri, 23 Aug 2025 12:30:45 GMT'
console.log(localized); // 'Sat, 23 Aug 2025 12:30:45 GMT'
```

### Using Compiled Patterns
Expand Down Expand Up @@ -141,11 +143,11 @@ The `transform()` function is a convenience wrapper that internally:

```typescript
// transform() is equivalent to:
const date = parse(dateString, sourceFormat);
const result = format(date, targetFormat);
const date = parse(dateString, sourceFormat, parserOptions);
const result = format(date, targetFormat, formatterOptions);

// But provides a cleaner API:
const result = transform(dateString, sourceFormat, targetFormat);
const result = transform(dateString, sourceFormat, targetFormat, parserOptions, formatterOptions);
```

## See Also
Expand Down
2 changes: 1 addition & 1 deletion docs/api/utils/getDaysInMonth.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: getDaysInMonth()
---

Returns the number of days in a given month of a specific year. Correctly handles leap years when calculating February.
Returns the number of days in a given month of a specific year. Correctly handles leap years when calculating February. When given a Date, its year and month are read in the local timezone.

## Syntax

Expand Down
Loading
Loading