@izonnet-web-solution/react-native-date-picker-kit 0.0.0-stage → 1.0.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.
Files changed (271) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/LICENSE +5 -0
  3. package/README.md +942 -2
  4. package/assets/screenshots/calendar-picker-blue.jpg +0 -0
  5. package/assets/screenshots/calendar-picker.jpg +0 -0
  6. package/assets/screenshots/datetime-picker-date.jpg +0 -0
  7. package/assets/screenshots/datetime-picker-time-blue.jpg +0 -0
  8. package/assets/screenshots/input-modal-picker.jpg +0 -0
  9. package/assets/screenshots/manual-entry-single.jpg +0 -0
  10. package/assets/screenshots/manual-entry-split.jpg +0 -0
  11. package/assets/screenshots/month-year-selector-modal.jpg +0 -0
  12. package/assets/screenshots/range-picker.jpg +0 -0
  13. package/assets/screenshots/time-12h-wheel.jpg +0 -0
  14. package/assets/screenshots/time-24h-military-blue.jpg +0 -0
  15. package/assets/screenshots/wheel-date-picker.jpg +0 -0
  16. package/lib/components/Calendar/Calendar.d.ts +35 -0
  17. package/lib/components/Calendar/Calendar.d.ts.map +1 -0
  18. package/lib/components/Calendar/Calendar.js +126 -0
  19. package/lib/components/Calendar/Calendar.js.map +1 -0
  20. package/lib/components/Calendar/CalendarGrid.d.ts +11 -0
  21. package/lib/components/Calendar/CalendarGrid.d.ts.map +1 -0
  22. package/lib/components/Calendar/CalendarGrid.js +22 -0
  23. package/lib/components/Calendar/CalendarGrid.js.map +1 -0
  24. package/lib/components/Calendar/CalendarHeader.d.ts +23 -0
  25. package/lib/components/Calendar/CalendarHeader.d.ts.map +1 -0
  26. package/lib/components/Calendar/CalendarHeader.js +62 -0
  27. package/lib/components/Calendar/CalendarHeader.js.map +1 -0
  28. package/lib/components/Calendar/DateCell.d.ts +11 -0
  29. package/lib/components/Calendar/DateCell.d.ts.map +1 -0
  30. package/lib/components/Calendar/DateCell.js +71 -0
  31. package/lib/components/Calendar/DateCell.js.map +1 -0
  32. package/lib/components/Calendar/MonthYearSelectorModal.d.ts +17 -0
  33. package/lib/components/Calendar/MonthYearSelectorModal.d.ts.map +1 -0
  34. package/lib/components/Calendar/MonthYearSelectorModal.js +253 -0
  35. package/lib/components/Calendar/MonthYearSelectorModal.js.map +1 -0
  36. package/lib/components/Calendar/WeekDays.d.ts +13 -0
  37. package/lib/components/Calendar/WeekDays.d.ts.map +1 -0
  38. package/lib/components/Calendar/WeekDays.js +34 -0
  39. package/lib/components/Calendar/WeekDays.js.map +1 -0
  40. package/lib/components/Calendar/index.d.ts +7 -0
  41. package/lib/components/Calendar/index.d.ts.map +1 -0
  42. package/lib/components/Calendar/index.js +23 -0
  43. package/lib/components/Calendar/index.js.map +1 -0
  44. package/lib/components/DatePicker.d.ts +4 -0
  45. package/lib/components/DatePicker.d.ts.map +1 -0
  46. package/lib/components/DatePicker.js +498 -0
  47. package/lib/components/DatePicker.js.map +1 -0
  48. package/lib/components/Input/CalendarPopup.d.ts +26 -0
  49. package/lib/components/Input/CalendarPopup.d.ts.map +1 -0
  50. package/lib/components/Input/CalendarPopup.js +60 -0
  51. package/lib/components/Input/CalendarPopup.js.map +1 -0
  52. package/lib/components/Input/DateInput.d.ts +29 -0
  53. package/lib/components/Input/DateInput.d.ts.map +1 -0
  54. package/lib/components/Input/DateInput.js +110 -0
  55. package/lib/components/Input/DateInput.js.map +1 -0
  56. package/lib/components/Input/index.d.ts +3 -0
  57. package/lib/components/Input/index.d.ts.map +1 -0
  58. package/lib/components/Input/index.js +19 -0
  59. package/lib/components/Input/index.js.map +1 -0
  60. package/lib/components/ManualDateInput/DateInputField.d.ts +19 -0
  61. package/lib/components/ManualDateInput/DateInputField.d.ts.map +1 -0
  62. package/lib/components/ManualDateInput/DateInputField.js +345 -0
  63. package/lib/components/ManualDateInput/DateInputField.js.map +1 -0
  64. package/lib/components/ManualDateInput/DateValidationMessage.d.ts +8 -0
  65. package/lib/components/ManualDateInput/DateValidationMessage.d.ts.map +1 -0
  66. package/lib/components/ManualDateInput/DateValidationMessage.js +45 -0
  67. package/lib/components/ManualDateInput/DateValidationMessage.js.map +1 -0
  68. package/lib/components/ManualDateInput/ManualDateInput.d.ts +19 -0
  69. package/lib/components/ManualDateInput/ManualDateInput.d.ts.map +1 -0
  70. package/lib/components/ManualDateInput/ManualDateInput.js +18 -0
  71. package/lib/components/ManualDateInput/ManualDateInput.js.map +1 -0
  72. package/lib/components/ManualDateInput/index.d.ts +4 -0
  73. package/lib/components/ManualDateInput/index.d.ts.map +1 -0
  74. package/lib/components/ManualDateInput/index.js +20 -0
  75. package/lib/components/ManualDateInput/index.js.map +1 -0
  76. package/lib/components/Range/RangeDateCell.d.ts +11 -0
  77. package/lib/components/Range/RangeDateCell.d.ts.map +1 -0
  78. package/lib/components/Range/RangeDateCell.js +107 -0
  79. package/lib/components/Range/RangeDateCell.js.map +1 -0
  80. package/lib/components/Range/RangePicker.d.ts +29 -0
  81. package/lib/components/Range/RangePicker.d.ts.map +1 -0
  82. package/lib/components/Range/RangePicker.js +261 -0
  83. package/lib/components/Range/RangePicker.js.map +1 -0
  84. package/lib/components/Range/index.d.ts +3 -0
  85. package/lib/components/Range/index.d.ts.map +1 -0
  86. package/lib/components/Range/index.js +19 -0
  87. package/lib/components/Range/index.js.map +1 -0
  88. package/lib/components/Time/ClockDial.d.ts +15 -0
  89. package/lib/components/Time/ClockDial.d.ts.map +1 -0
  90. package/lib/components/Time/ClockDial.js +347 -0
  91. package/lib/components/Time/ClockDial.js.map +1 -0
  92. package/lib/components/Time/DateTimePicker.d.ts +31 -0
  93. package/lib/components/Time/DateTimePicker.d.ts.map +1 -0
  94. package/lib/components/Time/DateTimePicker.js +197 -0
  95. package/lib/components/Time/DateTimePicker.js.map +1 -0
  96. package/lib/components/Time/TimePicker.d.ts +31 -0
  97. package/lib/components/Time/TimePicker.d.ts.map +1 -0
  98. package/lib/components/Time/TimePicker.js +550 -0
  99. package/lib/components/Time/TimePicker.js.map +1 -0
  100. package/lib/components/Time/WheelTimePicker.d.ts +27 -0
  101. package/lib/components/Time/WheelTimePicker.d.ts.map +1 -0
  102. package/lib/components/Time/WheelTimePicker.js +203 -0
  103. package/lib/components/Time/WheelTimePicker.js.map +1 -0
  104. package/lib/components/Time/index.d.ts +5 -0
  105. package/lib/components/Time/index.d.ts.map +1 -0
  106. package/lib/components/Time/index.js +21 -0
  107. package/lib/components/Time/index.js.map +1 -0
  108. package/lib/components/Wheel/WheelColumn.d.ts +17 -0
  109. package/lib/components/Wheel/WheelColumn.d.ts.map +1 -0
  110. package/lib/components/Wheel/WheelColumn.js +192 -0
  111. package/lib/components/Wheel/WheelColumn.js.map +1 -0
  112. package/lib/components/Wheel/WheelPicker.d.ts +26 -0
  113. package/lib/components/Wheel/WheelPicker.d.ts.map +1 -0
  114. package/lib/components/Wheel/WheelPicker.js +123 -0
  115. package/lib/components/Wheel/WheelPicker.js.map +1 -0
  116. package/lib/components/Wheel/index.d.ts +3 -0
  117. package/lib/components/Wheel/index.d.ts.map +1 -0
  118. package/lib/components/Wheel/index.js +19 -0
  119. package/lib/components/Wheel/index.js.map +1 -0
  120. package/lib/components/common/Button.d.ts +17 -0
  121. package/lib/components/common/Button.d.ts.map +1 -0
  122. package/lib/components/common/Button.js +77 -0
  123. package/lib/components/common/Button.js.map +1 -0
  124. package/lib/components/common/IconButton.d.ts +14 -0
  125. package/lib/components/common/IconButton.d.ts.map +1 -0
  126. package/lib/components/common/IconButton.js +150 -0
  127. package/lib/components/common/IconButton.js.map +1 -0
  128. package/lib/components/common/Modal.d.ts +13 -0
  129. package/lib/components/common/Modal.d.ts.map +1 -0
  130. package/lib/components/common/Modal.js +65 -0
  131. package/lib/components/common/Modal.js.map +1 -0
  132. package/lib/components/common/index.d.ts +4 -0
  133. package/lib/components/common/index.d.ts.map +1 -0
  134. package/lib/components/common/index.js +20 -0
  135. package/lib/components/common/index.js.map +1 -0
  136. package/lib/components/index.d.ts +9 -0
  137. package/lib/components/index.d.ts.map +1 -0
  138. package/lib/components/index.js +25 -0
  139. package/lib/components/index.js.map +1 -0
  140. package/lib/hooks/index.d.ts +4 -0
  141. package/lib/hooks/index.d.ts.map +1 -0
  142. package/lib/hooks/index.js +20 -0
  143. package/lib/hooks/index.js.map +1 -0
  144. package/lib/hooks/useCalendar.d.ts +34 -0
  145. package/lib/hooks/useCalendar.d.ts.map +1 -0
  146. package/lib/hooks/useCalendar.js +93 -0
  147. package/lib/hooks/useCalendar.js.map +1 -0
  148. package/lib/hooks/useDatePicker.d.ts +28 -0
  149. package/lib/hooks/useDatePicker.d.ts.map +1 -0
  150. package/lib/hooks/useDatePicker.js +150 -0
  151. package/lib/hooks/useDatePicker.js.map +1 -0
  152. package/lib/hooks/useDateRange.d.ts +23 -0
  153. package/lib/hooks/useDateRange.d.ts.map +1 -0
  154. package/lib/hooks/useDateRange.js +82 -0
  155. package/lib/hooks/useDateRange.js.map +1 -0
  156. package/lib/index.d.ts +7 -0
  157. package/lib/index.d.ts.map +1 -0
  158. package/lib/index.js +29 -0
  159. package/lib/index.js.map +1 -0
  160. package/lib/localization/en.d.ts +3 -0
  161. package/lib/localization/en.d.ts.map +1 -0
  162. package/lib/localization/en.js +80 -0
  163. package/lib/localization/en.js.map +1 -0
  164. package/lib/localization/gu.d.ts +3 -0
  165. package/lib/localization/gu.d.ts.map +1 -0
  166. package/lib/localization/gu.js +80 -0
  167. package/lib/localization/gu.js.map +1 -0
  168. package/lib/localization/hi.d.ts +3 -0
  169. package/lib/localization/hi.d.ts.map +1 -0
  170. package/lib/localization/hi.js +80 -0
  171. package/lib/localization/hi.js.map +1 -0
  172. package/lib/localization/index.d.ts +8 -0
  173. package/lib/localization/index.d.ts.map +1 -0
  174. package/lib/localization/index.js +32 -0
  175. package/lib/localization/index.js.map +1 -0
  176. package/lib/themes/dark.d.ts +3 -0
  177. package/lib/themes/dark.d.ts.map +1 -0
  178. package/lib/themes/dark.js +30 -0
  179. package/lib/themes/dark.js.map +1 -0
  180. package/lib/themes/index.d.ts +16 -0
  181. package/lib/themes/index.d.ts.map +1 -0
  182. package/lib/themes/index.js +56 -0
  183. package/lib/themes/index.js.map +1 -0
  184. package/lib/themes/light.d.ts +3 -0
  185. package/lib/themes/light.d.ts.map +1 -0
  186. package/lib/themes/light.js +30 -0
  187. package/lib/themes/light.js.map +1 -0
  188. package/lib/types/index.d.ts +344 -0
  189. package/lib/types/index.d.ts.map +1 -0
  190. package/lib/types/index.js +3 -0
  191. package/lib/types/index.js.map +1 -0
  192. package/lib/utils/calendarUtils.d.ts +27 -0
  193. package/lib/utils/calendarUtils.d.ts.map +1 -0
  194. package/lib/utils/calendarUtils.js +108 -0
  195. package/lib/utils/calendarUtils.js.map +1 -0
  196. package/lib/utils/dateFormatting.d.ts +29 -0
  197. package/lib/utils/dateFormatting.d.ts.map +1 -0
  198. package/lib/utils/dateFormatting.js +114 -0
  199. package/lib/utils/dateFormatting.js.map +1 -0
  200. package/lib/utils/dateParsing.d.ts +23 -0
  201. package/lib/utils/dateParsing.d.ts.map +1 -0
  202. package/lib/utils/dateParsing.js +289 -0
  203. package/lib/utils/dateParsing.js.map +1 -0
  204. package/lib/utils/dateUtils.d.ts +20 -0
  205. package/lib/utils/dateUtils.d.ts.map +1 -0
  206. package/lib/utils/dateUtils.js +115 -0
  207. package/lib/utils/dateUtils.js.map +1 -0
  208. package/lib/utils/dateValidation.d.ts +36 -0
  209. package/lib/utils/dateValidation.d.ts.map +1 -0
  210. package/lib/utils/dateValidation.js +196 -0
  211. package/lib/utils/dateValidation.js.map +1 -0
  212. package/lib/utils/index.d.ts +7 -0
  213. package/lib/utils/index.d.ts.map +1 -0
  214. package/lib/utils/index.js +23 -0
  215. package/lib/utils/index.js.map +1 -0
  216. package/lib/utils/timeUtils.d.ts +78 -0
  217. package/lib/utils/timeUtils.d.ts.map +1 -0
  218. package/lib/utils/timeUtils.js +335 -0
  219. package/lib/utils/timeUtils.js.map +1 -0
  220. package/package.json +92 -3
  221. package/src/components/Calendar/Calendar.tsx +314 -0
  222. package/src/components/Calendar/CalendarGrid.tsx +47 -0
  223. package/src/components/Calendar/CalendarHeader.tsx +176 -0
  224. package/src/components/Calendar/DateCell.tsx +118 -0
  225. package/src/components/Calendar/MonthYearSelectorModal.tsx +407 -0
  226. package/src/components/Calendar/WeekDays.tsx +49 -0
  227. package/src/components/Calendar/index.ts +6 -0
  228. package/src/components/DatePicker.tsx +834 -0
  229. package/src/components/Input/CalendarPopup.tsx +168 -0
  230. package/src/components/Input/DateInput.tsx +259 -0
  231. package/src/components/Input/index.ts +2 -0
  232. package/src/components/ManualDateInput/DateInputField.tsx +493 -0
  233. package/src/components/ManualDateInput/DateValidationMessage.tsx +65 -0
  234. package/src/components/ManualDateInput/ManualDateInput.tsx +66 -0
  235. package/src/components/ManualDateInput/index.ts +3 -0
  236. package/src/components/Range/RangeDateCell.tsx +174 -0
  237. package/src/components/Range/RangePicker.tsx +484 -0
  238. package/src/components/Range/index.ts +2 -0
  239. package/src/components/Time/ClockDial.tsx +450 -0
  240. package/src/components/Time/DateTimePicker.tsx +395 -0
  241. package/src/components/Time/TimePicker.tsx +899 -0
  242. package/src/components/Time/WheelTimePicker.tsx +356 -0
  243. package/src/components/Time/index.ts +4 -0
  244. package/src/components/Wheel/WheelColumn.tsx +300 -0
  245. package/src/components/Wheel/WheelPicker.tsx +265 -0
  246. package/src/components/Wheel/index.ts +2 -0
  247. package/src/components/common/Button.tsx +125 -0
  248. package/src/components/common/IconButton.tsx +237 -0
  249. package/src/components/common/Modal.tsx +137 -0
  250. package/src/components/common/index.ts +3 -0
  251. package/src/components/index.ts +8 -0
  252. package/src/hooks/index.ts +3 -0
  253. package/src/hooks/useCalendar.ts +153 -0
  254. package/src/hooks/useDatePicker.ts +216 -0
  255. package/src/hooks/useDateRange.ts +135 -0
  256. package/src/index.ts +17 -0
  257. package/src/localization/en.ts +78 -0
  258. package/src/localization/gu.ts +78 -0
  259. package/src/localization/hi.ts +78 -0
  260. package/src/localization/index.ts +31 -0
  261. package/src/themes/dark.ts +28 -0
  262. package/src/themes/index.ts +73 -0
  263. package/src/themes/light.ts +28 -0
  264. package/src/types/index.ts +452 -0
  265. package/src/utils/calendarUtils.ts +152 -0
  266. package/src/utils/dateFormatting.ts +125 -0
  267. package/src/utils/dateParsing.ts +335 -0
  268. package/src/utils/dateUtils.ts +117 -0
  269. package/src/utils/dateValidation.ts +276 -0
  270. package/src/utils/index.ts +6 -0
  271. package/src/utils/timeUtils.ts +416 -0
@@ -0,0 +1,125 @@
1
+ import type { DateFormat, DatePickerLocale, SupportedLocale, StandardDateFormat } from '../types';
2
+
3
+ export const STANDARD_DATE_FORMATS: StandardDateFormat[] = [
4
+ 'DD/MM/YYYY',
5
+ 'MM/DD/YYYY',
6
+ 'YYYY/DD/MM',
7
+ 'YYYY/MM/DD',
8
+ ];
9
+
10
+ export const DateFormats = {
11
+ DD_MM_YYYY: 'DD/MM/YYYY',
12
+ MM_DD_YYYY: 'MM/DD/YYYY',
13
+ YYYY_DD_MM: 'YYYY/DD/MM',
14
+ YYYY_MM_DD: 'YYYY/MM/DD',
15
+ YYYY_MM_DD_DASH: 'YYYY-MM-DD',
16
+ DD_MM_YYYY_DASH: 'DD-MM-YYYY',
17
+ HH_MM: 'HH:mm',
18
+ HH_MM_SS: 'HH:mm:ss',
19
+ HH_MM_A: 'hh:mm A',
20
+ HH_MM_SS_A: 'hh:mm:ss A',
21
+ DATETIME_24H: 'DD/MM/YYYY HH:mm',
22
+ DATETIME_12H: 'DD/MM/YYYY hh:mm A',
23
+ } as const;
24
+ import { enLocale } from '../localization/en';
25
+ import { getLocale } from '../localization';
26
+ import { convert24To12, padZero } from './timeUtils';
27
+
28
+ /**
29
+ * Format a Date object to string based on format pattern and locale.
30
+ * Supports date, 24-hour, and 12-hour AM/PM formats.
31
+ * Safely accepts locale as a string code ('en', 'hi', 'gu') or a DatePickerLocale object.
32
+ * Uses a single-pass regex replacement to completely prevent token collision.
33
+ * Never throws on valid Date.
34
+ */
35
+ export const formatDate = (
36
+ date: Date | null | undefined,
37
+ format: DateFormat | string = 'DD/MM/YYYY',
38
+ locale: SupportedLocale | DatePickerLocale = enLocale
39
+ ): string => {
40
+ if (!date || isNaN(date.getTime())) {
41
+ return '';
42
+ }
43
+
44
+ const activeLocale = getLocale(locale);
45
+
46
+ const year = date.getFullYear();
47
+ const month = date.getMonth(); // 0-indexed
48
+ const day = date.getDate();
49
+ const dayOfWeek = date.getDay();
50
+
51
+ const hours24 = date.getHours();
52
+ const minutes = date.getMinutes();
53
+ const seconds = date.getSeconds();
54
+
55
+ const { hour12, period } = convert24To12(hours24);
56
+ const localizedPeriod =
57
+ period === 'AM' ? activeLocale.am || 'AM' : activeLocale.pm || 'PM';
58
+
59
+ const tokenMap: Record<string, string> = {
60
+ YYYY: String(year),
61
+ YY: String(year).slice(-2),
62
+ MMMM: activeLocale.months?.[month] || '',
63
+ MMM: activeLocale.monthsShort?.[month] || '',
64
+ MM: padZero(month + 1),
65
+ M: String(month + 1),
66
+ DD: padZero(day),
67
+ D: String(day),
68
+ dddd: activeLocale.weekdays?.[dayOfWeek] || '',
69
+ ddd: activeLocale.weekdaysShort?.[dayOfWeek] || '',
70
+ HH: padZero(hours24),
71
+ H: String(hours24),
72
+ hh: padZero(hour12),
73
+ h: String(hour12),
74
+ mm: padZero(minutes),
75
+ m: String(minutes),
76
+ ss: padZero(seconds),
77
+ s: String(seconds),
78
+ A: localizedPeriod,
79
+ a: localizedPeriod.toLowerCase(),
80
+ };
81
+
82
+ // Match tokens or bracket-escaped strings in a single pass
83
+ const regex = /(\[.*?\]|YYYY|YY|MMMM|MMM|MM|M|dddd|ddd|DD|D|HH|H|hh|h|mm|m|ss|s|A|a)/g;
84
+
85
+ return format.replace(regex, (match) => {
86
+ if (match.startsWith('[') && match.endsWith(']')) {
87
+ return match.slice(1, -1);
88
+ }
89
+ return tokenMap[match] !== undefined ? tokenMap[match] : match;
90
+ });
91
+ };
92
+
93
+ /**
94
+ * Returns user-friendly placeholder based on format
95
+ */
96
+ export const getPlaceholderForFormat = (format: DateFormat | string = 'DD/MM/YYYY'): string => {
97
+ switch (format) {
98
+ case 'DD/MM/YYYY':
99
+ return 'DD/MM/YYYY';
100
+ case 'MM/DD/YYYY':
101
+ return 'MM/DD/YYYY';
102
+ case 'YYYY/DD/MM':
103
+ return 'YYYY/DD/MM';
104
+ case 'YYYY/MM/DD':
105
+ return 'YYYY/MM/DD';
106
+ case 'YYYY-MM-DD':
107
+ return 'YYYY-MM-DD';
108
+ case 'DD-MM-YYYY':
109
+ return 'DD-MM-YYYY';
110
+ case 'HH:mm':
111
+ return 'HH:mm (24h)';
112
+ case 'HH:mm:ss':
113
+ return 'HH:mm:ss (24h)';
114
+ case 'hh:mm A':
115
+ return 'hh:mm AM/PM';
116
+ case 'hh:mm:ss A':
117
+ return 'hh:mm:ss AM/PM';
118
+ case 'DD/MM/YYYY HH:mm':
119
+ return 'DD/MM/YYYY HH:mm';
120
+ case 'DD/MM/YYYY hh:mm A':
121
+ return 'DD/MM/YYYY hh:mm AM/PM';
122
+ default:
123
+ return format;
124
+ }
125
+ };
@@ -0,0 +1,335 @@
1
+ import type { DateFormat, DatePickerLocale, SupportedLocale, ValidationResult } from '../types';
2
+ import { enLocale } from '../localization/en';
3
+ import { getLocale } from '../localization';
4
+ import { validateDateComponents } from './dateValidation';
5
+ import { createLocalDate } from './dateUtils';
6
+ import {
7
+ parseTimeString,
8
+ validateTime,
9
+ is24HourFormat,
10
+ setTimeOnDate,
11
+ } from './timeUtils';
12
+
13
+ export interface ParseResult {
14
+ date: Date | null;
15
+ validation: ValidationResult;
16
+ }
17
+
18
+ /**
19
+ * Strict time string parsing according to format.
20
+ * Supports 24-hour (HH:mm, HH:mm:ss) and 12-hour (hh:mm A, hh:mm:ss A).
21
+ */
22
+ export const parseTimeStrict = (
23
+ text: string,
24
+ format: DateFormat | string = 'hh:mm A',
25
+ locale: SupportedLocale | DatePickerLocale = enLocale,
26
+ baseDate: Date = new Date()
27
+ ): ParseResult => {
28
+ const activeLocale = getLocale(locale);
29
+ const trimmed = text.trim();
30
+ if (!trimmed) {
31
+ return {
32
+ date: null,
33
+ validation: {
34
+ isValid: false,
35
+ errorCode: 'INVALID_FORMAT',
36
+ error: activeLocale.errors.invalidTime || 'Please enter a valid time.',
37
+ },
38
+ };
39
+ }
40
+
41
+ const is24 = is24HourFormat(format);
42
+ const parsed = parseTimeString(trimmed);
43
+
44
+ if (!parsed) {
45
+ return {
46
+ date: null,
47
+ validation: {
48
+ isValid: false,
49
+ errorCode: 'INVALID_FORMAT',
50
+ error: activeLocale.errors.invalidTime || 'Please enter a valid time.',
51
+ },
52
+ };
53
+ }
54
+
55
+ const validation = validateTime(
56
+ {
57
+ hours: parsed.hours,
58
+ minutes: parsed.minutes,
59
+ seconds: parsed.seconds,
60
+ period: parsed.period,
61
+ is24Hour: parsed.is24Hour,
62
+ },
63
+ is24,
64
+ { locale: activeLocale }
65
+ );
66
+
67
+ if (!validation.isValid) {
68
+ return {
69
+ date: null,
70
+ validation,
71
+ };
72
+ }
73
+
74
+ const resultDate = setTimeOnDate(
75
+ baseDate,
76
+ parsed.hours24,
77
+ parsed.minutes,
78
+ parsed.seconds
79
+ );
80
+
81
+ return {
82
+ date: resultDate,
83
+ validation: { isValid: true },
84
+ };
85
+ };
86
+
87
+ /**
88
+ * Strict date string parsing according to format.
89
+ * Supports date-only, time-only, and combined datetime formats.
90
+ * Never calls new Date(text) with ambiguous strings.
91
+ * Never silently normalizes impossible dates.
92
+ */
93
+ export const parseDateStrict = (
94
+ text: string,
95
+ format: DateFormat | string = 'DD/MM/YYYY',
96
+ locale: SupportedLocale | DatePickerLocale = enLocale
97
+ ): ParseResult => {
98
+ const activeLocale = getLocale(locale);
99
+ const trimmed = text.trim();
100
+ if (!trimmed) {
101
+ return {
102
+ date: null,
103
+ validation: {
104
+ isValid: false,
105
+ errorCode: 'INVALID_FORMAT',
106
+ error: activeLocale.errors.invalidDate,
107
+ },
108
+ };
109
+ }
110
+
111
+ const hasDatePart = /Y|D|M/i.test(format.replace(/mm/g, ''));
112
+ const hasTimePart = /H|h|m|s|[Aa]/.test(format);
113
+
114
+ // 1. Time-only format (e.g. HH:mm, hh:mm A)
115
+ if (!hasDatePart && hasTimePart) {
116
+ return parseTimeStrict(trimmed, format, activeLocale);
117
+ }
118
+
119
+ // 2. DateTime format (e.g. DD/MM/YYYY HH:mm, DD/MM/YYYY hh:mm A)
120
+ if (hasDatePart && hasTimePart) {
121
+ let dateStr = '';
122
+ let timeStr = '';
123
+ let dateFormatStr = '';
124
+ let timeFormatStr = '';
125
+
126
+ if (format.includes(' ')) {
127
+ const formatSpaceIdx = format.indexOf(' ');
128
+ dateFormatStr = format.slice(0, formatSpaceIdx).trim();
129
+ timeFormatStr = format.slice(formatSpaceIdx + 1).trim();
130
+
131
+ const textSpaceIdx = trimmed.indexOf(' ');
132
+ if (textSpaceIdx > 0) {
133
+ dateStr = trimmed.slice(0, textSpaceIdx).trim();
134
+ timeStr = trimmed.slice(textSpaceIdx + 1).trim();
135
+ }
136
+ } else if (format.includes('T')) {
137
+ const formatParts = format.split('T');
138
+ dateFormatStr = formatParts[0];
139
+ timeFormatStr = formatParts[1];
140
+ const textParts = trimmed.split('T');
141
+ dateStr = textParts[0];
142
+ timeStr = textParts[1] || '';
143
+ }
144
+
145
+ if (!dateStr || !timeStr) {
146
+ return {
147
+ date: null,
148
+ validation: {
149
+ isValid: false,
150
+ errorCode: 'INVALID_FORMAT',
151
+ error: activeLocale.errors.invalidDate,
152
+ },
153
+ };
154
+ }
155
+
156
+ const dateResult = parseDateStrict(dateStr, dateFormatStr, activeLocale);
157
+ if (!dateResult.validation.isValid || !dateResult.date) {
158
+ return dateResult;
159
+ }
160
+
161
+ const timeResult = parseTimeStrict(timeStr, timeFormatStr, activeLocale, dateResult.date);
162
+ return timeResult;
163
+ }
164
+
165
+ // 3. Date-only format (DD/MM/YYYY, MM/DD/YYYY, YYYY-MM-DD, DD-MM-YYYY)
166
+ let separator = '/';
167
+ if (format.includes('-')) {
168
+ separator = '-';
169
+ } else if (format.includes('.')) {
170
+ separator = '.';
171
+ }
172
+
173
+ const parts = trimmed.split(separator);
174
+ const formatParts = format.split(separator);
175
+
176
+ if (parts.length !== 3 || formatParts.length !== 3) {
177
+ return {
178
+ date: null,
179
+ validation: {
180
+ isValid: false,
181
+ errorCode: 'INVALID_FORMAT',
182
+ error: activeLocale.errors.invalidDate,
183
+ },
184
+ };
185
+ }
186
+
187
+ let day: number | null = null;
188
+ let month: number | null = null;
189
+ let year: number | null = null;
190
+
191
+ for (let i = 0; i < 3; i++) {
192
+ const part = parts[i].trim();
193
+ const token = formatParts[i].trim().toUpperCase();
194
+
195
+ // Verify all chars are numeric
196
+ if (!/^\d+$/.test(part)) {
197
+ return {
198
+ date: null,
199
+ validation: {
200
+ isValid: false,
201
+ errorCode: 'INVALID_FORMAT',
202
+ error: activeLocale.errors.invalidDate,
203
+ },
204
+ };
205
+ }
206
+
207
+ const num = parseInt(part, 10);
208
+
209
+ if (token.startsWith('D')) {
210
+ if (part.length > 2) {
211
+ return {
212
+ date: null,
213
+ validation: {
214
+ isValid: false,
215
+ errorCode: 'INVALID_DAY',
216
+ error: activeLocale.errors.invalidDate,
217
+ },
218
+ };
219
+ }
220
+ day = num;
221
+ } else if (token.startsWith('M')) {
222
+ if (part.length > 2) {
223
+ return {
224
+ date: null,
225
+ validation: {
226
+ isValid: false,
227
+ errorCode: 'INVALID_MONTH',
228
+ error: activeLocale.errors.invalidDate,
229
+ },
230
+ };
231
+ }
232
+ month = num;
233
+ } else if (token.startsWith('Y')) {
234
+ if (token === 'YYYY' && part.length !== 4) {
235
+ return {
236
+ date: null,
237
+ validation: {
238
+ isValid: false,
239
+ errorCode: 'INVALID_YEAR',
240
+ error: activeLocale.errors.invalidDate,
241
+ },
242
+ };
243
+ }
244
+ if (token === 'YY' && part.length !== 2) {
245
+ return {
246
+ date: null,
247
+ validation: {
248
+ isValid: false,
249
+ errorCode: 'INVALID_YEAR',
250
+ error: activeLocale.errors.invalidDate,
251
+ },
252
+ };
253
+ }
254
+ year = token === 'YY' ? 2000 + num : num;
255
+ }
256
+ }
257
+
258
+ if (day === null || month === null || year === null) {
259
+ return {
260
+ date: null,
261
+ validation: {
262
+ isValid: false,
263
+ errorCode: 'INVALID_FORMAT',
264
+ error: activeLocale.errors.invalidDate,
265
+ },
266
+ };
267
+ }
268
+
269
+ // Strictly validate day, month, leap year, days in month
270
+ const componentValidation = validateDateComponents(year, month, day, activeLocale);
271
+ if (!componentValidation.isValid) {
272
+ return {
273
+ date: null,
274
+ validation: componentValidation,
275
+ };
276
+ }
277
+
278
+ try {
279
+ const constructedDate = createLocalDate(year, month - 1, day);
280
+ return {
281
+ date: constructedDate,
282
+ validation: { isValid: true },
283
+ };
284
+ } catch (err: any) {
285
+ return {
286
+ date: null,
287
+ validation: {
288
+ isValid: false,
289
+ errorCode: 'INVALID_FORMAT',
290
+ error: err?.message || activeLocale.errors.invalidDate,
291
+ },
292
+ };
293
+ }
294
+ };
295
+
296
+ /**
297
+ * Automatically applies mask separators as the user types digits.
298
+ * Supports date and time formats.
299
+ */
300
+ export const formatMaskedInput = (
301
+ rawText: string,
302
+ format: DateFormat = 'DD/MM/YYYY'
303
+ ): string => {
304
+ // Time format mask (HH:mm)
305
+ if (format === 'HH:mm') {
306
+ const digitsOnly = rawText.replace(/\D/g, '');
307
+ const h = digitsOnly.slice(0, 2);
308
+ const m = digitsOnly.slice(2, 4);
309
+ if (m) return `${h}:${m}`;
310
+ return h;
311
+ }
312
+
313
+ const digitsOnly = rawText.replace(/\D/g, '');
314
+ const separator = format.includes('-') ? '-' : '/';
315
+
316
+ if (format.startsWith('YYYY')) {
317
+ // YYYY-MM-DD
318
+ const y = digitsOnly.slice(0, 4);
319
+ const m = digitsOnly.slice(4, 6);
320
+ const d = digitsOnly.slice(6, 8);
321
+
322
+ if (d) return `${y}${separator}${m}${separator}${d}`;
323
+ if (m) return `${y}${separator}${m}`;
324
+ return y;
325
+ } else {
326
+ // DD/MM/YYYY or MM/DD/YYYY or DD-MM-YYYY
327
+ const p1 = digitsOnly.slice(0, 2);
328
+ const p2 = digitsOnly.slice(2, 4);
329
+ const p3 = digitsOnly.slice(4, 8);
330
+
331
+ if (p3) return `${p1}${separator}${p2}${separator}${p3}`;
332
+ if (p2) return `${p1}${separator}${p2}`;
333
+ return p1;
334
+ }
335
+ };
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Robust date utility functions using local calendar units.
3
+ * Never compares dates using formatted strings or ambiguous date string parsing.
4
+ */
5
+
6
+ export const isLeapYear = (year: number): boolean => {
7
+ if (!Number.isInteger(year) || year < 1) {
8
+ return false;
9
+ }
10
+ return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
11
+ };
12
+
13
+ export const getDaysInMonth = (year: number, monthIndex: number): number => {
14
+ if (monthIndex < 0 || monthIndex > 11) {
15
+ return 0;
16
+ }
17
+ const daysPerMonth = [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
18
+ return daysPerMonth[monthIndex];
19
+ };
20
+
21
+ export const createLocalDate = (year: number, monthIndex: number, day: number): Date => {
22
+ const date = new Date(year, monthIndex, day, 0, 0, 0, 0);
23
+ // Guard against JavaScript date rollover (e.g. Feb 30 -> March 2)
24
+ if (
25
+ date.getFullYear() !== year ||
26
+ date.getMonth() !== monthIndex ||
27
+ date.getDate() !== day
28
+ ) {
29
+ throw new Error(`Invalid calendar date components: ${year}-${monthIndex + 1}-${day}`);
30
+ }
31
+ return date;
32
+ };
33
+
34
+ export const stripTime = (date: Date): Date => {
35
+ return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0, 0);
36
+ };
37
+
38
+ export const isSameDay = (date1: Date | null | undefined, date2: Date | null | undefined): boolean => {
39
+ if (!date1 || !date2 || isNaN(date1.getTime()) || isNaN(date2.getTime())) {
40
+ return false;
41
+ }
42
+ return (
43
+ date1.getFullYear() === date2.getFullYear() &&
44
+ date1.getMonth() === date2.getMonth() &&
45
+ date1.getDate() === date2.getDate()
46
+ );
47
+ };
48
+
49
+ export const isBeforeDay = (date1: Date, date2: Date): boolean => {
50
+ const d1 = stripTime(date1);
51
+ const d2 = stripTime(date2);
52
+ return d1.getTime() < d2.getTime();
53
+ };
54
+
55
+ export const isAfterDay = (date1: Date, date2: Date): boolean => {
56
+ const d1 = stripTime(date1);
57
+ const d2 = stripTime(date2);
58
+ return d1.getTime() > d2.getTime();
59
+ };
60
+
61
+ export const isDateInRange = (date: Date, startDate: Date | null, endDate: Date | null): boolean => {
62
+ if (!startDate || !endDate) {
63
+ return false;
64
+ }
65
+ const d = stripTime(date).getTime();
66
+ const start = stripTime(startDate).getTime();
67
+ const end = stripTime(endDate).getTime();
68
+ return d >= start && d <= end;
69
+ };
70
+
71
+ export const addDays = (date: Date, amount: number): Date => {
72
+ const result = new Date(date);
73
+ result.setDate(result.getDate() + amount);
74
+ return result;
75
+ };
76
+
77
+ export const addMonths = (date: Date, amount: number): Date => {
78
+ const year = date.getFullYear();
79
+ const month = date.getMonth();
80
+ const day = date.getDate();
81
+
82
+ const targetDate = new Date(year, month + amount, 1);
83
+ const targetYear = targetDate.getFullYear();
84
+ const targetMonth = targetDate.getMonth();
85
+ const maxDays = getDaysInMonth(targetYear, targetMonth);
86
+
87
+ return new Date(targetYear, targetMonth, Math.min(day, maxDays), 0, 0, 0, 0);
88
+ };
89
+
90
+ export const addYears = (date: Date, amount: number): Date => {
91
+ const year = date.getFullYear() + amount;
92
+ const month = date.getMonth();
93
+ const day = date.getDate();
94
+ const maxDays = getDaysInMonth(year, month);
95
+ return new Date(year, month, Math.min(day, maxDays), 0, 0, 0, 0);
96
+ };
97
+
98
+ export const getStartOfMonth = (date: Date): Date => {
99
+ return new Date(date.getFullYear(), date.getMonth(), 1, 0, 0, 0, 0);
100
+ };
101
+
102
+ export const getEndOfMonth = (date: Date): Date => {
103
+ const year = date.getFullYear();
104
+ const month = date.getMonth();
105
+ return new Date(year, month, getDaysInMonth(year, month), 23, 59, 59, 999);
106
+ };
107
+
108
+ export const getDaysDifference = (start: Date, end: Date): number => {
109
+ const s = stripTime(start).getTime();
110
+ const e = stripTime(end).getTime();
111
+ const msInDay = 86400000;
112
+ return Math.round(Math.abs(e - s) / msInDay) + 1; // inclusive count
113
+ };
114
+
115
+ export const isToday = (date: Date): boolean => {
116
+ return isSameDay(date, new Date());
117
+ };