|
1 | | -# Date Formatting |
| 1 | +# Date & DateTime Formatting |
2 | 2 |
|
3 | | -Format and validate dates with support for various formats and locales. |
| 3 | +Format and validate dates with support for various formats, time components, and locales. |
4 | 4 |
|
5 | 5 | ## Basic Usage |
6 | 6 |
|
7 | 7 | ```typescript |
8 | | -import { formatDate, isValidDate, parseDate } from 'ts-inputs' |
9 | | - |
10 | | -// Format a date |
11 | | -const formattedDate = formatDate(new Date(), 'YYYY-MM-DD') |
12 | | -// Output: '2024-03-21' |
| 8 | +import { BaseInput } from 'ts-inputs' |
| 9 | + |
| 10 | +// Create a date input |
| 11 | +const dateInput = new BaseInput('#date-input', { |
| 12 | + dateTime: true, |
| 13 | + dateTimeOptions: { |
| 14 | + format: 'YYYY-MM-DD', |
| 15 | + showSeconds: false, |
| 16 | + is24Hour: true, |
| 17 | + }, |
| 18 | + onDateTimeFormatChanged: (formatted) => { |
| 19 | + console.log('Formatted date:', formatted) |
| 20 | + }, |
| 21 | + onDateTimeValidityChanged: (isValid) => { |
| 22 | + console.log('Is valid date:', isValid) |
| 23 | + }, |
| 24 | +}) |
13 | 25 |
|
14 | | -// Parse a date string |
15 | | -const parsedDate = parseDate('2024-03-21', 'YYYY-MM-DD') |
16 | | -// Output: Date object |
| 26 | +// Get the formatted value |
| 27 | +const formattedValue = dateInput.getValue() |
| 28 | +// Example: '2024-03-21' |
17 | 29 |
|
18 | | -// Validate a date |
19 | | -const isValid = isValidDate('2024-03-21') |
20 | | -// Output: true |
| 30 | +// Get the unformatted value (numeric only) |
| 31 | +const unformattedValue = dateInput.getUnformattedValue() |
| 32 | +// Example: '20240321' |
21 | 33 | ``` |
22 | 34 |
|
23 | | -## Date Formats |
24 | | - |
25 | | -The library supports various date formats: |
| 35 | +## Date Formats & Options |
26 | 36 |
|
27 | | -- `YYYY-MM-DD` (ISO format) |
28 | | -- `MM/DD/YYYY` (US format) |
29 | | -- `DD/MM/YYYY` (European format) |
30 | | -- `YYYY/MM/DD` (Japanese format) |
31 | | - |
32 | | -## Locale Support |
| 37 | +The datetime input supports various configuration options: |
33 | 38 |
|
34 | 39 | ```typescript |
35 | | -import { formatDate, setLocale } from 'ts-inputs' |
| 40 | +const dateTimeInput = new BaseInput('#date-time-input', { |
| 41 | + dateTime: true, |
| 42 | + dateTimeOptions: { |
| 43 | + // Date format options |
| 44 | + format: 'YYYY-MM-DD HH:mm:ss', // Full format |
| 45 | + dateFormat: 'YYYY-MM-DD', // Date part format |
| 46 | + timeFormat: 'HH:mm:ss', // Time part format |
| 47 | + dateSeparator: '-', // Date component separator |
| 48 | + timeSeparator: ':', // Time component separator |
| 49 | + separator: ' ', // Separator between date and time |
| 50 | + |
| 51 | + // Time format options |
| 52 | + is24Hour: true, // Use 24-hour format |
| 53 | + showSeconds: true, // Show seconds in time |
| 54 | + |
| 55 | + // Validation options |
| 56 | + strictMode: true, // Enforce strict validation |
| 57 | + minDate: '2024-01-01', // Minimum allowed date |
| 58 | + maxDate: '2024-12-31', // Maximum allowed date |
| 59 | + disabledDates: ['2024-03-15'], // Disabled dates |
| 60 | + |
| 61 | + // Other options |
| 62 | + locale: 'en-US', // Locale for formatting |
| 63 | + allowEmpty: true, // Allow empty input |
| 64 | + placeholder: 'Enter date...', // Input placeholder |
| 65 | + }, |
| 66 | +}) |
| 67 | +``` |
36 | 68 |
|
37 | | -// Set locale |
38 | | -setLocale('en-US') |
| 69 | +## Validation Features |
39 | 70 |
|
40 | | -// Format with locale |
41 | | -const formattedDate = formatDate(new Date(), 'LL', { locale: 'en-US' }) |
42 | | -// Output: 'March 21, 2024' |
| 71 | +The datetime input includes comprehensive validation: |
| 72 | + |
| 73 | +```typescript |
| 74 | +const dateInput = new BaseInput('#validated-date-input', { |
| 75 | + dateTime: true, |
| 76 | + dateTimeOptions: { |
| 77 | + strictMode: true, |
| 78 | + minDate: new Date('2024-01-01'), |
| 79 | + maxDate: new Date('2024-12-31'), |
| 80 | + disabledDates: [ |
| 81 | + '2024-03-15', |
| 82 | + '2024-03-16', |
| 83 | + ], |
| 84 | + }, |
| 85 | + onDateTimeValidityChanged: (isValid) => { |
| 86 | + if (!isValid) { |
| 87 | + console.log('Invalid date selected') |
| 88 | + } |
| 89 | + }, |
| 90 | +}) |
43 | 91 | ``` |
44 | 92 |
|
45 | | -## Date Validation |
| 93 | +## Direct API Usage |
| 94 | + |
| 95 | +You can also use the formatting functions directly: |
46 | 96 |
|
47 | 97 | ```typescript |
48 | | -import { isValidDate, validateDate } from 'ts-inputs' |
| 98 | +import { formatDateTime, isValidDateTime, unformatDateTime } from 'ts-inputs' |
49 | 99 |
|
50 | | -// Simple validation |
51 | | -const isValid = isValidDate('2024-03-21') |
| 100 | +// Format a date string |
| 101 | +const formatted = formatDateTime('20240321', { |
| 102 | + format: 'YYYY-MM-DD', |
| 103 | + strictMode: true, |
| 104 | +}) |
| 105 | +// Output: '2024-03-21' |
52 | 106 |
|
53 | | -// Advanced validation with options |
54 | | -const validation = validateDate('2024-03-21', { |
55 | | - minDate: new Date('2024-01-01'), |
56 | | - maxDate: new Date('2024-12-31'), |
57 | | - format: 'YYYY-MM-DD' |
| 107 | +// Validate a date |
| 108 | +const isValid = isValidDateTime('2024-03-21', { |
| 109 | + minDate: '2024-01-01', |
| 110 | + maxDate: '2024-12-31', |
58 | 111 | }) |
| 112 | +// Output: true |
| 113 | + |
| 114 | +// Get unformatted value |
| 115 | +const unformatted = unformatDateTime('2024-03-21') |
| 116 | +// Output: '20240321' |
59 | 117 | ``` |
60 | 118 |
|
61 | 119 | ## Error Handling |
62 | 120 |
|
63 | | -```typescript |
64 | | -try { |
65 | | - const formattedDate = formatDate('invalid-date', 'YYYY-MM-DD') |
66 | | -} |
67 | | -catch (error) { |
68 | | - console.error('Invalid date:', error.message) |
69 | | -} |
70 | | -``` |
| 121 | +The input handles various edge cases gracefully: |
| 122 | + |
| 123 | +- Invalid dates are prevented in strict mode |
| 124 | +- Out-of-range values are handled according to validation rules |
| 125 | +- Empty values are allowed when `allowEmpty` is true |
| 126 | +- Disabled dates are prevented from being selected |
| 127 | +- Min/max date constraints are enforced |
0 commit comments