ngxsmk-datepicker 2.3.1 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +942 -922
- package/docs/API.md +2423 -2413
- package/docs/COMPATIBILITY.md +471 -471
- package/docs/INTEGRATION.md +703 -703
- package/docs/IONIC_INTEGRATION.md +228 -228
- package/docs/LOCALE-GUIDE.md +300 -300
- package/docs/PLUGIN-ARCHITECTURE.md +930 -930
- package/docs/SSR-EXAMPLE.md +427 -427
- package/docs/THEME-TOKENS.md +324 -324
- package/docs/TIMEZONE.md +307 -307
- package/docs/extension-points.md +419 -419
- package/docs/signal-forms.md +600 -600
- package/docs/signals.md +266 -266
- package/docs/ssr.md +305 -305
- package/package.json +106 -104
- package/schematics/collection.json +10 -0
- package/schematics/ng-add/schema.json +14 -0
- package/schematics/tsconfig.json +17 -0
- package/CHANGELOG.md +0 -1246
- package/LICENSE +0 -21
- package/MIGRATION.md +0 -1794
- package/docs/FEATURE_SCOPING.md +0 -60
- package/docs/IONIC_TESTING.md +0 -148
- package/docs/REFACTOR_PLAN.md +0 -38
- package/docs/SEO.md +0 -214
- package/fesm2022/ngxsmk-datepicker.mjs +0 -14693
- package/types/ngxsmk-datepicker.d.ts +0 -2489
package/docs/TIMEZONE.md
CHANGED
|
@@ -1,307 +1,307 @@
|
|
|
1
|
-
# Timezone Support
|
|
2
|
-
|
|
3
|
-
**Last updated:**
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
The `ngxsmk-datepicker` library provides timezone-aware date formatting and parsing. This document explains how timezones work in the context of JavaScript Date objects and how to use the library's timezone features.
|
|
8
|
-
|
|
9
|
-
## Understanding JavaScript Date Objects
|
|
10
|
-
|
|
11
|
-
**Important**: JavaScript `Date` objects are **always stored in UTC internally**. When you create a `Date`, it represents a specific moment in time (a UTC timestamp). However, when you display or format a date, it is automatically converted to the user's local timezone.
|
|
12
|
-
|
|
13
|
-
### Key Concepts
|
|
14
|
-
|
|
15
|
-
1. **Storage**: All dates are stored as UTC timestamps (milliseconds since January 1, 1970 UTC)
|
|
16
|
-
2. **Display**: Dates are formatted in a specific timezone (defaults to user's local timezone)
|
|
17
|
-
3. **Parsing**: When parsing date strings, the interpretation depends on whether timezone information is included
|
|
18
|
-
|
|
19
|
-
### Example
|
|
20
|
-
|
|
21
|
-
```typescript
|
|
22
|
-
// Create a date (stored as UTC internally)
|
|
23
|
-
const date = new Date('2024-01-15T10:00:00Z'); // Z indicates UTC
|
|
24
|
-
|
|
25
|
-
// Display in different timezones
|
|
26
|
-
date.toLocaleString('en-US', { timeZone: 'America/New_York' }); // "1/15/2024, 5:00:00 AM"
|
|
27
|
-
date.toLocaleString('en-US', { timeZone: 'Europe/London' }); // "1/15/2024, 10:00:00 AM"
|
|
28
|
-
date.toLocaleString('en-US', { timeZone: 'Asia/Tokyo' }); // "1/15/2024, 7:00:00 PM"
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Using Timezone Support
|
|
32
|
-
|
|
33
|
-
### Component-Level Timezone
|
|
34
|
-
|
|
35
|
-
Set the `timezone` input property on individual datepicker components:
|
|
36
|
-
|
|
37
|
-
```typescript
|
|
38
|
-
<ngxsmk-datepicker
|
|
39
|
-
mode="single"
|
|
40
|
-
[timezone]="'America/New_York'"
|
|
41
|
-
[showTime]="true">
|
|
42
|
-
</ngxsmk-datepicker>
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
### Global Timezone Configuration
|
|
46
|
-
|
|
47
|
-
Set a default timezone for all datepicker instances using the configuration provider:
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
import { provideDatepickerConfig } from 'ngxsmk-datepicker';
|
|
51
|
-
|
|
52
|
-
export const appConfig: ApplicationConfig = {
|
|
53
|
-
providers: [
|
|
54
|
-
provideDatepickerConfig({
|
|
55
|
-
timezone: 'America/New_York',
|
|
56
|
-
// ... other config
|
|
57
|
-
})
|
|
58
|
-
]
|
|
59
|
-
};
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
### Timezone Format
|
|
63
|
-
|
|
64
|
-
The timezone must be a valid IANA timezone name. Common examples:
|
|
65
|
-
|
|
66
|
-
- `'UTC'` - Coordinated Universal Time
|
|
67
|
-
- `'America/New_York'` - Eastern Time (US)
|
|
68
|
-
- `'America/Los_Angeles'` - Pacific Time (US)
|
|
69
|
-
- `'Europe/London'` - British Time
|
|
70
|
-
- `'Europe/Paris'` - Central European Time
|
|
71
|
-
- `'Asia/Tokyo'` - Japan Standard Time
|
|
72
|
-
- `'Australia/Sydney'` - Australian Eastern Time
|
|
73
|
-
|
|
74
|
-
You can find a complete list of IANA timezone names at: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
75
|
-
|
|
76
|
-
## How the Library Handles Timezones
|
|
77
|
-
|
|
78
|
-
### Date Storage
|
|
79
|
-
|
|
80
|
-
The library **always stores dates as JavaScript Date objects** (UTC internally). The timezone setting only affects:
|
|
81
|
-
|
|
82
|
-
1. **Display formatting**: How dates are shown to users
|
|
83
|
-
2. **Input parsing**: How date strings are interpreted (when timezone info is missing)
|
|
84
|
-
|
|
85
|
-
### Date Selection
|
|
86
|
-
|
|
87
|
-
When a user selects a date/time in the calendar:
|
|
88
|
-
|
|
89
|
-
1. The selected date/time is interpreted in the **specified timezone** (or local timezone if not specified)
|
|
90
|
-
2. It is converted to a JavaScript `Date` object (stored as UTC)
|
|
91
|
-
3. When displayed, it is formatted back to the specified timezone
|
|
92
|
-
|
|
93
|
-
### Example Flow
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
// User selects: January 15, 2024 at 10:00 AM in 'America/New_York' timezone
|
|
97
|
-
// 1. Library creates: new Date('2024-01-15T10:00:00-05:00')
|
|
98
|
-
// (Stored internally as UTC: 2024-01-15T15:00:00Z)
|
|
99
|
-
// 2. When displayed with timezone='America/New_York':
|
|
100
|
-
// Shows: "Jan 15, 2024, 10:00 AM"
|
|
101
|
-
// 3. When displayed with timezone='Europe/London':
|
|
102
|
-
// Shows: "Jan 15, 2024, 3:00 PM" (5 hours ahead)
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
## Best Practices
|
|
106
|
-
|
|
107
|
-
### 1. Always Store UTC Dates
|
|
108
|
-
|
|
109
|
-
When saving dates to a database or API, always send the UTC timestamp:
|
|
110
|
-
|
|
111
|
-
```typescript
|
|
112
|
-
const selectedDate: Date = datepicker.value; // Already UTC internally
|
|
113
|
-
const utcTimestamp = selectedDate.toISOString(); // "2024-01-15T15:00:00.000Z"
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
### 2. Use Timezone for Display Only
|
|
117
|
-
|
|
118
|
-
Set the timezone based on your user's location or preference, but remember that the underlying Date object is always UTC:
|
|
119
|
-
|
|
120
|
-
```typescript
|
|
121
|
-
// Display in user's preferred timezone
|
|
122
|
-
<ngxsmk-datepicker
|
|
123
|
-
[timezone]="userTimezone"
|
|
124
|
-
(valueChange)="onDateChange($event)">
|
|
125
|
-
</ngxsmk-datepicker>
|
|
126
|
-
|
|
127
|
-
onDateChange(date: Date) {
|
|
128
|
-
// Date is UTC internally - send to API as-is
|
|
129
|
-
this.apiService.saveDate(date.toISOString());
|
|
130
|
-
}
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
### 3. Handle Server Dates
|
|
134
|
-
|
|
135
|
-
When receiving dates from a server (usually UTC), create Date objects directly:
|
|
136
|
-
|
|
137
|
-
```typescript
|
|
138
|
-
// Server returns: "2024-01-15T15:00:00Z"
|
|
139
|
-
const serverDate = new Date("2024-01-15T15:00:00Z");
|
|
140
|
-
|
|
141
|
-
// Display in user's timezone
|
|
142
|
-
<ngxsmk-datepicker
|
|
143
|
-
[value]="serverDate"
|
|
144
|
-
[timezone]="userTimezone">
|
|
145
|
-
</ngxsmk-datepicker>
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
## Optional Adapters (date-fns / Luxon)
|
|
149
|
-
|
|
150
|
-
For advanced timezone operations, you may want to use specialized libraries:
|
|
151
|
-
|
|
152
|
-
### date-fns-tz
|
|
153
|
-
|
|
154
|
-
```typescript
|
|
155
|
-
import { formatInTimeZone, zonedTimeToUtc } from 'date-fns-tz';
|
|
156
|
-
|
|
157
|
-
// Format in specific timezone
|
|
158
|
-
const formatted = formatInTimeZone(date, 'America/New_York', 'yyyy-MM-dd HH:mm');
|
|
159
|
-
|
|
160
|
-
// Convert zoned time to UTC
|
|
161
|
-
const utcDate = zonedTimeToUtc('2024-01-15 10:00', 'America/New_York');
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### Luxon
|
|
165
|
-
|
|
166
|
-
```typescript
|
|
167
|
-
import { DateTime } from 'luxon';
|
|
168
|
-
|
|
169
|
-
// Create date in specific timezone
|
|
170
|
-
const dt = DateTime.fromObject(
|
|
171
|
-
{ year: 2024, month: 1, day: 15, hour: 10 },
|
|
172
|
-
{ zone: 'America/New_York' }
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
// Convert to UTC
|
|
176
|
-
const utc = dt.toUTC();
|
|
177
|
-
|
|
178
|
-
// Format
|
|
179
|
-
const formatted = dt.toFormat('yyyy-MM-dd HH:mm');
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
### Integration with ngxsmk-datepicker
|
|
183
|
-
|
|
184
|
-
You can use these libraries alongside ngxsmk-datepicker:
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
import { formatInTimeZone } from 'date-fns-tz';
|
|
188
|
-
|
|
189
|
-
@Component({
|
|
190
|
-
template: `
|
|
191
|
-
<ngxsmk-datepicker
|
|
192
|
-
[value]="date"
|
|
193
|
-
(valueChange)="onDateChange($event)">
|
|
194
|
-
</ngxsmk-datepicker>
|
|
195
|
-
|
|
196
|
-
<p>Formatted: {{ formatDate(date) }}</p>
|
|
197
|
-
`
|
|
198
|
-
})
|
|
199
|
-
export class MyComponent {
|
|
200
|
-
date: Date | null = null;
|
|
201
|
-
|
|
202
|
-
formatDate(date: Date | null): string {
|
|
203
|
-
if (!date) return '';
|
|
204
|
-
return formatInTimeZone(date, 'America/New_York', 'PPP p');
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
onDateChange(date: Date | null) {
|
|
208
|
-
this.date = date;
|
|
209
|
-
// Date is UTC - use with date-fns-tz or Luxon as needed
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
## Common Issues and Solutions
|
|
215
|
-
|
|
216
|
-
### Issue: Dates appear in wrong timezone
|
|
217
|
-
|
|
218
|
-
**Solution**: Ensure you're setting the `timezone` property correctly:
|
|
219
|
-
|
|
220
|
-
```typescript
|
|
221
|
-
// ✅ Correct
|
|
222
|
-
<ngxsmk-datepicker [timezone]="'America/New_York'">
|
|
223
|
-
|
|
224
|
-
// ❌ Incorrect (missing quotes)
|
|
225
|
-
<ngxsmk-datepicker [timezone]="America/New_York">
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
### Issue: Dates shift when saving to server
|
|
229
|
-
|
|
230
|
-
**Solution**: JavaScript Date objects are already UTC. Send them directly:
|
|
231
|
-
|
|
232
|
-
```typescript
|
|
233
|
-
// ✅ Correct
|
|
234
|
-
const utcString = date.toISOString(); // Send this to server
|
|
235
|
-
|
|
236
|
-
// ❌ Incorrect (don't manually adjust)
|
|
237
|
-
const adjusted = new Date(date.getTime() - timezoneOffset); // Don't do this
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
### Issue: Parsing dates from server
|
|
241
|
-
|
|
242
|
-
**Solution**: Create Date objects from ISO strings:
|
|
243
|
-
|
|
244
|
-
```typescript
|
|
245
|
-
// ✅ Correct
|
|
246
|
-
const date = new Date(serverResponse.dateString); // "2024-01-15T15:00:00Z"
|
|
247
|
-
|
|
248
|
-
// ❌ Incorrect (may parse in wrong timezone)
|
|
249
|
-
const date = new Date(serverResponse.dateString.replace('Z', '')); // Don't remove Z
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
## API Reference
|
|
253
|
-
|
|
254
|
-
### Input: `timezone`
|
|
255
|
-
|
|
256
|
-
- **Type**: `string | undefined`
|
|
257
|
-
- **Default**: `undefined` (uses browser's local timezone)
|
|
258
|
-
- **Description**: IANA timezone name for date formatting
|
|
259
|
-
- **Example**: `'America/New_York'`, `'UTC'`, `'Europe/London'`
|
|
260
|
-
|
|
261
|
-
### Input: `showTimezoneSelector`
|
|
262
|
-
|
|
263
|
-
- **Type**: `boolean`
|
|
264
|
-
- **Default**: `false`
|
|
265
|
-
- **Description**: Enables a searchable timezone selection dropdown UI in the datepicker header.
|
|
266
|
-
- **Example**: `[showTimezoneSelector]="true"`
|
|
267
|
-
|
|
268
|
-
### Output: `timezoneChange`
|
|
269
|
-
|
|
270
|
-
- **Type**: `EventEmitter<string>`
|
|
271
|
-
- **Description**: Emitted when the user changes the active timezone via the selector UI.
|
|
272
|
-
- **Example**: `(timezoneChange)="onTimezoneChange($event)"`
|
|
273
|
-
|
|
274
|
-
### Utility Functions
|
|
275
|
-
|
|
276
|
-
The library exports timezone utility functions:
|
|
277
|
-
|
|
278
|
-
```typescript
|
|
279
|
-
import {
|
|
280
|
-
formatDateWithTimezone,
|
|
281
|
-
parseDateWithTimezone,
|
|
282
|
-
isValidTimezone
|
|
283
|
-
} from 'ngxsmk-datepicker';
|
|
284
|
-
|
|
285
|
-
// Format date with timezone
|
|
286
|
-
const formatted = formatDateWithTimezone(
|
|
287
|
-
date,
|
|
288
|
-
'en-US',
|
|
289
|
-
{ year: 'numeric', month: 'short', day: '2-digit' },
|
|
290
|
-
'America/New_York'
|
|
291
|
-
);
|
|
292
|
-
|
|
293
|
-
// Check if timezone is valid
|
|
294
|
-
if (isValidTimezone('America/New_York')) {
|
|
295
|
-
// Use timezone
|
|
296
|
-
}
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
## Summary
|
|
300
|
-
|
|
301
|
-
- JavaScript Date objects are **always UTC internally**
|
|
302
|
-
- The `timezone` property controls **display formatting only**
|
|
303
|
-
- Use IANA timezone names (e.g., `'America/New_York'`)
|
|
304
|
-
- For advanced operations, consider date-fns-tz or Luxon
|
|
305
|
-
- Always send UTC timestamps to servers/APIs
|
|
306
|
-
|
|
307
|
-
|
|
1
|
+
# Timezone Support
|
|
2
|
+
|
|
3
|
+
**Last updated:** July 2, 2026 - **Current stable:** v2.4.0
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The `ngxsmk-datepicker` library provides timezone-aware date formatting and parsing. This document explains how timezones work in the context of JavaScript Date objects and how to use the library's timezone features.
|
|
8
|
+
|
|
9
|
+
## Understanding JavaScript Date Objects
|
|
10
|
+
|
|
11
|
+
**Important**: JavaScript `Date` objects are **always stored in UTC internally**. When you create a `Date`, it represents a specific moment in time (a UTC timestamp). However, when you display or format a date, it is automatically converted to the user's local timezone.
|
|
12
|
+
|
|
13
|
+
### Key Concepts
|
|
14
|
+
|
|
15
|
+
1. **Storage**: All dates are stored as UTC timestamps (milliseconds since January 1, 1970 UTC)
|
|
16
|
+
2. **Display**: Dates are formatted in a specific timezone (defaults to user's local timezone)
|
|
17
|
+
3. **Parsing**: When parsing date strings, the interpretation depends on whether timezone information is included
|
|
18
|
+
|
|
19
|
+
### Example
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
// Create a date (stored as UTC internally)
|
|
23
|
+
const date = new Date('2024-01-15T10:00:00Z'); // Z indicates UTC
|
|
24
|
+
|
|
25
|
+
// Display in different timezones
|
|
26
|
+
date.toLocaleString('en-US', { timeZone: 'America/New_York' }); // "1/15/2024, 5:00:00 AM"
|
|
27
|
+
date.toLocaleString('en-US', { timeZone: 'Europe/London' }); // "1/15/2024, 10:00:00 AM"
|
|
28
|
+
date.toLocaleString('en-US', { timeZone: 'Asia/Tokyo' }); // "1/15/2024, 7:00:00 PM"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Using Timezone Support
|
|
32
|
+
|
|
33
|
+
### Component-Level Timezone
|
|
34
|
+
|
|
35
|
+
Set the `timezone` input property on individual datepicker components:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
<ngxsmk-datepicker
|
|
39
|
+
mode="single"
|
|
40
|
+
[timezone]="'America/New_York'"
|
|
41
|
+
[showTime]="true">
|
|
42
|
+
</ngxsmk-datepicker>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Global Timezone Configuration
|
|
46
|
+
|
|
47
|
+
Set a default timezone for all datepicker instances using the configuration provider:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { provideDatepickerConfig } from 'ngxsmk-datepicker';
|
|
51
|
+
|
|
52
|
+
export const appConfig: ApplicationConfig = {
|
|
53
|
+
providers: [
|
|
54
|
+
provideDatepickerConfig({
|
|
55
|
+
timezone: 'America/New_York',
|
|
56
|
+
// ... other config
|
|
57
|
+
})
|
|
58
|
+
]
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Timezone Format
|
|
63
|
+
|
|
64
|
+
The timezone must be a valid IANA timezone name. Common examples:
|
|
65
|
+
|
|
66
|
+
- `'UTC'` - Coordinated Universal Time
|
|
67
|
+
- `'America/New_York'` - Eastern Time (US)
|
|
68
|
+
- `'America/Los_Angeles'` - Pacific Time (US)
|
|
69
|
+
- `'Europe/London'` - British Time
|
|
70
|
+
- `'Europe/Paris'` - Central European Time
|
|
71
|
+
- `'Asia/Tokyo'` - Japan Standard Time
|
|
72
|
+
- `'Australia/Sydney'` - Australian Eastern Time
|
|
73
|
+
|
|
74
|
+
You can find a complete list of IANA timezone names at: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
75
|
+
|
|
76
|
+
## How the Library Handles Timezones
|
|
77
|
+
|
|
78
|
+
### Date Storage
|
|
79
|
+
|
|
80
|
+
The library **always stores dates as JavaScript Date objects** (UTC internally). The timezone setting only affects:
|
|
81
|
+
|
|
82
|
+
1. **Display formatting**: How dates are shown to users
|
|
83
|
+
2. **Input parsing**: How date strings are interpreted (when timezone info is missing)
|
|
84
|
+
|
|
85
|
+
### Date Selection
|
|
86
|
+
|
|
87
|
+
When a user selects a date/time in the calendar:
|
|
88
|
+
|
|
89
|
+
1. The selected date/time is interpreted in the **specified timezone** (or local timezone if not specified)
|
|
90
|
+
2. It is converted to a JavaScript `Date` object (stored as UTC)
|
|
91
|
+
3. When displayed, it is formatted back to the specified timezone
|
|
92
|
+
|
|
93
|
+
### Example Flow
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// User selects: January 15, 2024 at 10:00 AM in 'America/New_York' timezone
|
|
97
|
+
// 1. Library creates: new Date('2024-01-15T10:00:00-05:00')
|
|
98
|
+
// (Stored internally as UTC: 2024-01-15T15:00:00Z)
|
|
99
|
+
// 2. When displayed with timezone='America/New_York':
|
|
100
|
+
// Shows: "Jan 15, 2024, 10:00 AM"
|
|
101
|
+
// 3. When displayed with timezone='Europe/London':
|
|
102
|
+
// Shows: "Jan 15, 2024, 3:00 PM" (5 hours ahead)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Best Practices
|
|
106
|
+
|
|
107
|
+
### 1. Always Store UTC Dates
|
|
108
|
+
|
|
109
|
+
When saving dates to a database or API, always send the UTC timestamp:
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
const selectedDate: Date = datepicker.value; // Already UTC internally
|
|
113
|
+
const utcTimestamp = selectedDate.toISOString(); // "2024-01-15T15:00:00.000Z"
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 2. Use Timezone for Display Only
|
|
117
|
+
|
|
118
|
+
Set the timezone based on your user's location or preference, but remember that the underlying Date object is always UTC:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
// Display in user's preferred timezone
|
|
122
|
+
<ngxsmk-datepicker
|
|
123
|
+
[timezone]="userTimezone"
|
|
124
|
+
(valueChange)="onDateChange($event)">
|
|
125
|
+
</ngxsmk-datepicker>
|
|
126
|
+
|
|
127
|
+
onDateChange(date: Date) {
|
|
128
|
+
// Date is UTC internally - send to API as-is
|
|
129
|
+
this.apiService.saveDate(date.toISOString());
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 3. Handle Server Dates
|
|
134
|
+
|
|
135
|
+
When receiving dates from a server (usually UTC), create Date objects directly:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
// Server returns: "2024-01-15T15:00:00Z"
|
|
139
|
+
const serverDate = new Date("2024-01-15T15:00:00Z");
|
|
140
|
+
|
|
141
|
+
// Display in user's timezone
|
|
142
|
+
<ngxsmk-datepicker
|
|
143
|
+
[value]="serverDate"
|
|
144
|
+
[timezone]="userTimezone">
|
|
145
|
+
</ngxsmk-datepicker>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Optional Adapters (date-fns / Luxon)
|
|
149
|
+
|
|
150
|
+
For advanced timezone operations, you may want to use specialized libraries:
|
|
151
|
+
|
|
152
|
+
### date-fns-tz
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
import { formatInTimeZone, zonedTimeToUtc } from 'date-fns-tz';
|
|
156
|
+
|
|
157
|
+
// Format in specific timezone
|
|
158
|
+
const formatted = formatInTimeZone(date, 'America/New_York', 'yyyy-MM-dd HH:mm');
|
|
159
|
+
|
|
160
|
+
// Convert zoned time to UTC
|
|
161
|
+
const utcDate = zonedTimeToUtc('2024-01-15 10:00', 'America/New_York');
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Luxon
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import { DateTime } from 'luxon';
|
|
168
|
+
|
|
169
|
+
// Create date in specific timezone
|
|
170
|
+
const dt = DateTime.fromObject(
|
|
171
|
+
{ year: 2024, month: 1, day: 15, hour: 10 },
|
|
172
|
+
{ zone: 'America/New_York' }
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
// Convert to UTC
|
|
176
|
+
const utc = dt.toUTC();
|
|
177
|
+
|
|
178
|
+
// Format
|
|
179
|
+
const formatted = dt.toFormat('yyyy-MM-dd HH:mm');
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Integration with ngxsmk-datepicker
|
|
183
|
+
|
|
184
|
+
You can use these libraries alongside ngxsmk-datepicker:
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { formatInTimeZone } from 'date-fns-tz';
|
|
188
|
+
|
|
189
|
+
@Component({
|
|
190
|
+
template: `
|
|
191
|
+
<ngxsmk-datepicker
|
|
192
|
+
[value]="date"
|
|
193
|
+
(valueChange)="onDateChange($event)">
|
|
194
|
+
</ngxsmk-datepicker>
|
|
195
|
+
|
|
196
|
+
<p>Formatted: {{ formatDate(date) }}</p>
|
|
197
|
+
`
|
|
198
|
+
})
|
|
199
|
+
export class MyComponent {
|
|
200
|
+
date: Date | null = null;
|
|
201
|
+
|
|
202
|
+
formatDate(date: Date | null): string {
|
|
203
|
+
if (!date) return '';
|
|
204
|
+
return formatInTimeZone(date, 'America/New_York', 'PPP p');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
onDateChange(date: Date | null) {
|
|
208
|
+
this.date = date;
|
|
209
|
+
// Date is UTC - use with date-fns-tz or Luxon as needed
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Common Issues and Solutions
|
|
215
|
+
|
|
216
|
+
### Issue: Dates appear in wrong timezone
|
|
217
|
+
|
|
218
|
+
**Solution**: Ensure you're setting the `timezone` property correctly:
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
// ✅ Correct
|
|
222
|
+
<ngxsmk-datepicker [timezone]="'America/New_York'">
|
|
223
|
+
|
|
224
|
+
// ❌ Incorrect (missing quotes)
|
|
225
|
+
<ngxsmk-datepicker [timezone]="America/New_York">
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Issue: Dates shift when saving to server
|
|
229
|
+
|
|
230
|
+
**Solution**: JavaScript Date objects are already UTC. Send them directly:
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
// ✅ Correct
|
|
234
|
+
const utcString = date.toISOString(); // Send this to server
|
|
235
|
+
|
|
236
|
+
// ❌ Incorrect (don't manually adjust)
|
|
237
|
+
const adjusted = new Date(date.getTime() - timezoneOffset); // Don't do this
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Issue: Parsing dates from server
|
|
241
|
+
|
|
242
|
+
**Solution**: Create Date objects from ISO strings:
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
// ✅ Correct
|
|
246
|
+
const date = new Date(serverResponse.dateString); // "2024-01-15T15:00:00Z"
|
|
247
|
+
|
|
248
|
+
// ❌ Incorrect (may parse in wrong timezone)
|
|
249
|
+
const date = new Date(serverResponse.dateString.replace('Z', '')); // Don't remove Z
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## API Reference
|
|
253
|
+
|
|
254
|
+
### Input: `timezone`
|
|
255
|
+
|
|
256
|
+
- **Type**: `string | undefined`
|
|
257
|
+
- **Default**: `undefined` (uses browser's local timezone)
|
|
258
|
+
- **Description**: IANA timezone name for date formatting
|
|
259
|
+
- **Example**: `'America/New_York'`, `'UTC'`, `'Europe/London'`
|
|
260
|
+
|
|
261
|
+
### Input: `showTimezoneSelector`
|
|
262
|
+
|
|
263
|
+
- **Type**: `boolean`
|
|
264
|
+
- **Default**: `false`
|
|
265
|
+
- **Description**: Enables a searchable timezone selection dropdown UI in the datepicker header.
|
|
266
|
+
- **Example**: `[showTimezoneSelector]="true"`
|
|
267
|
+
|
|
268
|
+
### Output: `timezoneChange`
|
|
269
|
+
|
|
270
|
+
- **Type**: `EventEmitter<string>`
|
|
271
|
+
- **Description**: Emitted when the user changes the active timezone via the selector UI.
|
|
272
|
+
- **Example**: `(timezoneChange)="onTimezoneChange($event)"`
|
|
273
|
+
|
|
274
|
+
### Utility Functions
|
|
275
|
+
|
|
276
|
+
The library exports timezone utility functions:
|
|
277
|
+
|
|
278
|
+
```typescript
|
|
279
|
+
import {
|
|
280
|
+
formatDateWithTimezone,
|
|
281
|
+
parseDateWithTimezone,
|
|
282
|
+
isValidTimezone
|
|
283
|
+
} from 'ngxsmk-datepicker';
|
|
284
|
+
|
|
285
|
+
// Format date with timezone
|
|
286
|
+
const formatted = formatDateWithTimezone(
|
|
287
|
+
date,
|
|
288
|
+
'en-US',
|
|
289
|
+
{ year: 'numeric', month: 'short', day: '2-digit' },
|
|
290
|
+
'America/New_York'
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
// Check if timezone is valid
|
|
294
|
+
if (isValidTimezone('America/New_York')) {
|
|
295
|
+
// Use timezone
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## Summary
|
|
300
|
+
|
|
301
|
+
- JavaScript Date objects are **always UTC internally**
|
|
302
|
+
- The `timezone` property controls **display formatting only**
|
|
303
|
+
- Use IANA timezone names (e.g., `'America/New_York'`)
|
|
304
|
+
- For advanced operations, consider date-fns-tz or Luxon
|
|
305
|
+
- Always send UTC timestamps to servers/APIs
|
|
306
|
+
|
|
307
|
+
|