Skip to content

Commit c54dc6e

Browse files
Merge pull request #116 from knowledgecode/develop
v4.2.0: Add timezone features and improve code organization
2 parents fd6d116 + 7b1c703 commit c54dc6e

27 files changed

+2760
-374
lines changed

docs/api/format.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,33 @@ For a complete list of all supported locales with import examples, see [Supporte
134134

135135
### timeZone
136136

137-
**Type**: `TimeZone | "UTC"`
137+
**Type**: `TimeZone | string`
138138
**Default**: `undefined` (local timezone)
139139

140-
Converts the date to the specified timezone before formatting.
140+
Converts the date to the specified timezone before formatting. You can specify a timezone in three ways:
141141

142142
```typescript
143143
import { format } from 'date-and-time';
144+
145+
// Method 1: Individual timezone import
144146
import Tokyo from 'date-and-time/timezones/Asia/Tokyo';
145147
import New_York from 'date-and-time/timezones/America/New_York';
146148

149+
// Method 2: Consolidated timezone import
150+
import { Tokyo as TokyoTZ, New_York as NY } from 'date-and-time/timezone';
151+
147152
const date = new Date();
148153

149-
// Format in Tokyo timezone
154+
// Using TimeZone object (individual import)
150155
format(date, 'YYYY-MM-DD HH:mm:ss [JST]', { timeZone: Tokyo });
151156
// => 2025-08-23 23:30:45 JST
152157

153-
// Format in New York timezone
154-
format(date, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: New_York });
158+
// Using TimeZone object (consolidated import)
159+
format(date, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: NY });
160+
// => 2025-08-23 09:30:45 EST
161+
162+
// Using IANA timezone name string (format only)
163+
format(date, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: 'America/New_York' });
155164
// => 2025-08-23 09:30:45 EST
156165

157166
// Format in UTC
@@ -314,16 +323,35 @@ format(date, '\\[YYYY-MM-DD HH:mm:ss\\]');
314323
```typescript
315324
import { format } from 'date-and-time';
316325
import ja from 'date-and-time/locales/ja';
326+
327+
// Method 1: Individual timezone import
317328
import Tokyo from 'date-and-time/timezones/Asia/Tokyo';
318329

330+
// Method 2: Consolidated timezone import (alternative)
331+
import { Tokyo as TokyoTZ } from 'date-and-time/timezone';
332+
319333
const date = new Date();
320334

321-
// Japanese with timezone
335+
// Japanese with timezone (using individual import)
322336
format(date, 'YYYY年MMMM月D日dddd Ah:mm:ss [GMT]Z', {
323337
timeZone: Tokyo,
324338
locale: ja
325339
});
326340
// => 2025年8月23日土曜日 午後11:30:45 GMT+0900
341+
342+
// Or using consolidated import
343+
format(date, 'YYYY年MMMM月D日dddd Ah:mm:ss [GMT]Z', {
344+
timeZone: TokyoTZ,
345+
locale: ja
346+
});
347+
// => 2025年8月23日土曜日 午後11:30:45 GMT+0900
348+
349+
// Or using IANA timezone name string
350+
format(date, 'YYYY年MMMM月D日dddd Ah:mm:ss [GMT]Z', {
351+
timeZone: 'Asia/Tokyo',
352+
locale: ja
353+
});
354+
// => 2025年8月23日土曜日 午後11:30:45 GMT+0900
327355
```
328356

329357
### Business and Technical Formats
@@ -342,7 +370,7 @@ format(date, 'ddd, DD MMM YYYY HH:mm:ss ZZ');
342370
// => Sat, 23 Aug 2025 14:30:45 +09:00
343371

344372
// Log timestamp
345-
format(date, '[YYYY-MM-DD HH:mm:ss.SSS]');
373+
format(date, '\\[YYYY-MM-DD HH:mm:ss.SSS]\\');
346374
// => [2025-08-23 14:30:45.123]
347375

348376
// File naming

docs/api/parse.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ parse('2025-08-23T14:30:00 +05:00', 'YYYY-MM-DD[T]HH:mm:ss ZZ', { timeZone: New_
170170
// => Fri Aug 23 2025 14:30:00 GMT+0500 (New_York timeZone is ignored)
171171
```
172172

173+
:::warning Important Difference from format()
174+
The `parse()` function only accepts TimeZone objects and the "UTC" string for the `timeZone` option. Unlike `format()`, which supports both TimeZone objects and IANA timezone name strings, `parse()` does not support string type timezone names.
175+
:::
176+
173177
For a complete list of all supported timezones with import examples, see [Supported Timezones](../timezones).
174178

175179
### numeral

docs/guide/quick-start.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,47 @@ For a complete list of all supported locales with import examples, see [Supporte
8383

8484
```typescript
8585
import { format, parse } from 'date-and-time';
86+
87+
// Method 1: Individual timezone imports
8688
import Tokyo from 'date-and-time/timezones/Asia/Tokyo';
8789
import New_York from 'date-and-time/timezones/America/New_York';
8890

91+
// Method 2: Consolidated timezone imports (alternative)
92+
import { Los_Angeles, London } from 'date-and-time/timezone';
93+
8994
const date = new Date();
9095

91-
// Format in different timezones
96+
// Using TimeZone objects (Method 1 - individual import)
9297
format(date, 'YYYY-MM-DD HH:mm:ss [JST]', { timeZone: Tokyo });
9398
// => 2025-08-23 23:30:45 JST
9499

95-
format(date, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: New_York });
100+
// Using TimeZone objects (Method 2 - consolidated import)
101+
format(date, 'YYYY-MM-DD HH:mm:ss [PST]', { timeZone: Los_Angeles });
102+
// => 2025-08-23 06:30:45 PST
103+
104+
// Using IANA timezone name strings (simplest approach)
105+
format(date, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: 'America/New_York' });
96106
// => 2025-08-23 09:30:45 EST
97107

98108
// UTC formatting
99109
format(date, 'YYYY-MM-DD HH:mm:ss [UTC]', { timeZone: 'UTC' });
100110
// => 2025-08-23 14:30:45 UTC
111+
112+
// Parsing in timezone (TimeZone objects only)
113+
parse('2025-08-23 23:30:45', 'YYYY-MM-DD HH:mm:ss', { timeZone: Tokyo });
114+
// => Fri Aug 23 2025 23:30:45 GMT+0900
101115
```
102116

117+
### Three Ways to Specify Timezones
118+
119+
The `format()` function supports three methods for specifying timezones:
120+
121+
1. **TimeZone objects via individual imports** - Type-safe but requires multiple import statements
122+
2. **TimeZone objects via consolidated imports** - Type-safe with better code organization
123+
3. **IANA timezone name strings** - Simplest approach, no imports needed for timezone modules
124+
125+
The `parse()` function only supports TimeZone objects and the "UTC" string for timezone specification.
126+
103127
For a complete list of all supported timezones with import examples, see [Supported Timezones](../timezones).
104128

105129
## Date Arithmetic

docs/index.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ features:
5050
```typescript
5151
import { format, parse, addDays } from 'date-and-time';
5252
import ja from 'date-and-time/locales/ja';
53-
import Tokyo from 'date-and-time/timezones/Asia/Tokyo';
53+
import { Tokyo } from 'date-and-time/timezone';
5454

5555
const now = new Date();
5656

@@ -62,10 +62,14 @@ format(now, 'YYYY/MM/DD HH:mm:ss');
6262
format(now, 'YYYY年M月D日(ddd)', { locale: ja });
6363
// => 2025年8月23日(金)
6464

65-
// Timezone-aware formatting
65+
// Timezone-aware formatting (using TimeZone object)
6666
format(now, 'YYYY-MM-DD HH:mm:ss [JST]', { timeZone: Tokyo });
6767
// => 2025-08-23 23:30:45 JST
6868

69+
// Timezone-aware formatting (using IANA timezone name string)
70+
format(now, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: 'America/New_York' });
71+
// => 2025-08-23 09:30:45 EST
72+
6973
// Parsing
7074
const date = parse('2025/08/23 14:30:45', 'YYYY/MM/DD HH:mm:ss');
7175
console.log(date)

docs/migration.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ format(new Date(), 'ddd, MMM DD YYYY hh:mm A [GMT]Z', { timeZone: 'UTC' });
3737
// => Fri, Jan 02 2015 07:14 AM GMT+0000
3838
```
3939

40-
Additionally, since the `timezone` plugin has been integrated into the main library, the `formatTZ` function is now obsolete. Timezones are now imported as modules rather than using `IANA time zone names` (except for UTC timezone).
40+
Additionally, since the `timezone` plugin has been integrated into the main library, the `formatTZ` function is now obsolete. In v4.0/4.1, timezones must be imported as TimeZone objects from timezone modules. IANA timezone name strings are not supported in this version (except for UTC timezone).
4141

4242
```typescript
4343
import { format } from 'date-and-time';
@@ -47,6 +47,57 @@ format(now, 'dddd, MMMM D, YYYY [at] H:mm:ss.SSS [GMT]ZZ', { timeZone: New_York
4747
// => Wednesday, July 23, 2025 at 3:28:27.443 GMT-04:00
4848
```
4949

50+
#### New in v4.2.0: Enhanced Timezone Support
51+
52+
In addition to TimeZone objects, the `format()` function now supports specifying timezones using IANA timezone name strings. This provides flexibility in how you work with timezones:
53+
54+
```typescript
55+
import { format } from 'date-and-time';
56+
57+
const now = new Date();
58+
59+
// Using IANA timezone name string (simplest)
60+
format(now, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: 'America/New_York' });
61+
// => 2025-08-23 09:30:45 EST
62+
63+
// Using TimeZone object (recommended for type safety)
64+
import New_York from 'date-and-time/timezones/America/New_York';
65+
format(now, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: New_York });
66+
// => 2025-08-23 09:30:45 EST
67+
68+
// Using consolidated import (recommended for multiple timezones)
69+
import { New_York as NY } from 'date-and-time/timezone';
70+
format(now, 'YYYY-MM-DD HH:mm:ss [EST]', { timeZone: NY });
71+
// => 2025-08-23 09:30:45 EST
72+
```
73+
74+
**Note**: The `parse()` function does not support string type timezone names. Only TimeZone objects and the "UTC" string are supported for parsing.
75+
76+
##### Consolidated Import
77+
78+
For better code organization when working with multiple timezones, you can import all timezones from a single `date-and-time/timezone` module:
79+
80+
```typescript
81+
// Consolidated import (recommended for multiple timezones)
82+
import { Tokyo, New_York, London, Sydney } from 'date-and-time/timezone';
83+
84+
const now = new Date();
85+
86+
format(now, 'YYYY-MM-DD HH:mm', { timeZone: Tokyo }); // JST
87+
format(now, 'YYYY-MM-DD HH:mm', { timeZone: New_York }); // EST/EDT
88+
format(now, 'YYYY-MM-DD HH:mm', { timeZone: London }); // GMT/BST
89+
format(now, 'YYYY-MM-DD HH:mm', { timeZone: Sydney }); // AEDT/AEST
90+
```
91+
92+
Alternatively, you can still import individual timezone modules directly:
93+
94+
```typescript
95+
import Tokyo from 'date-and-time/timezones/Asia/Tokyo';
96+
import New_York from 'date-and-time/timezones/America/New_York';
97+
import London from 'date-and-time/timezones/Europe/London';
98+
import Sydney from 'date-and-time/timezones/Australia/Sydney';
99+
```
100+
50101
### parse
51102

52103
The third argument has been changed from `boolean` to `ParserOptions`. With `ParserOptions`, you can now specify timezone and locale settings. If you previously set the third argument to `true` to parse input in UTC timezone, you can achieve the same output as follows:

docs/timezones.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,67 @@
11
# Supported Timezones
22

3+
## Import Methods
4+
5+
There are three ways to import and use timezones with the date-and-time library:
6+
7+
### Method 1: Individual Import
8+
9+
Import each timezone you need directly from its module path. This approach is useful when you only need a few specific timezones.
10+
11+
```typescript
12+
import { format } from 'date-and-time';
13+
import Tokyo from 'date-and-time/timezones/Asia/Tokyo';
14+
import New_York from 'date-and-time/timezones/America/New_York';
15+
16+
const date = new Date();
17+
18+
format(date, 'YYYY-MM-DD HH:mm:ss', { timeZone: Tokyo });
19+
// => 2025-08-23 23:30:45
20+
21+
format(date, 'YYYY-MM-DD HH:mm:ss', { timeZone: New_York });
22+
// => 2025-08-23 09:30:45
23+
```
24+
25+
### Method 2: Consolidated Import
26+
27+
Import multiple timezones from a single module using named imports. This approach is recommended when working with multiple timezones as it provides better code organization.
28+
29+
```typescript
30+
import { format } from 'date-and-time';
31+
import { Tokyo, New_York, London, Sydney } from 'date-and-time/timezone';
32+
33+
const date = new Date();
34+
35+
format(date, 'YYYY-MM-DD HH:mm:ss', { timeZone: Tokyo }); // JST
36+
format(date, 'YYYY-MM-DD HH:mm:ss', { timeZone: New_York }); // EST/EDT
37+
format(date, 'YYYY-MM-DD HH:mm:ss', { timeZone: London }); // GMT/BST
38+
format(date, 'YYYY-MM-DD HH:mm:ss', { timeZone: Sydney }); // AEDT/AEST
39+
```
40+
41+
Both Method 1 and Method 2 provide the same functionality - they differ only in code organization.
42+
43+
### Method 3: IANA Timezone Name String
44+
45+
Functions that accept `FormatterOptions` (such as `format()` and `transform()`) allow you to specify timezones using IANA timezone name strings directly. This is the simplest approach.
46+
47+
```typescript
48+
import { format } from 'date-and-time';
49+
50+
const date = new Date();
51+
52+
// Using IANA timezone name strings
53+
format(date, 'YYYY-MM-DD HH:mm:ss', { timeZone: 'Asia/Tokyo' });
54+
// => 2025-08-23 23:30:45
55+
56+
format(date, 'YYYY-MM-DD HH:mm:ss', { timeZone: 'America/New_York' });
57+
// => 2025-08-23 09:30:45
58+
59+
format(date, 'YYYY-MM-DD HH:mm:ss', { timeZone: 'Europe/London' });
60+
// => 2025-08-23 14:30:45
61+
```
62+
63+
**Important Note**: The `parse()` function does not support string type timezone names because it uses `ParserOptions` instead of `FormatterOptions`. Only TimeZone objects and the "UTC" string are supported for parsing.
64+
365
## Regions
466

567
1. [Africa](#africa)

0 commit comments

Comments
 (0)