@react-fabric/date 1.0.0-beta.5 → 1.0.0-beta.51
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 +2 -9
- package/{src/components/Date.module.css → css/_date.css} +5 -5
- package/{src/superdate/SuperDate.module.css → css/styles.css} +2 -8
- package/dist/cjs/components/DatePage.js +2 -3
- package/dist/cjs/components/TimeSliders.js +5 -6
- package/dist/cjs/display/DateDisplay.js +7 -2
- package/dist/cjs/display/DateDuration.js +127 -0
- package/dist/cjs/hijri/DateConverter.js +154 -0
- package/dist/cjs/hijri/FormatDate.js +115 -0
- package/dist/cjs/hijri/HijriDate.js +531 -0
- package/dist/cjs/hijri/StringDateParser.js +74 -0
- package/dist/cjs/hijri/index.js +17 -1025
- package/dist/cjs/hijri/initializer.js +113 -0
- package/dist/cjs/hijri/locales.js +19 -0
- package/dist/cjs/hijri/safe.js +17 -0
- package/dist/cjs/i18n/ar.json.js +191 -3
- package/dist/cjs/i18n/en.json.js +63 -1
- package/dist/cjs/i18n/index.js +0 -6
- package/dist/cjs/index.js +13 -2
- package/dist/cjs/{superdate → panels}/EventPanel.js +16 -4
- package/dist/cjs/utils/dateUtil.js +3 -3
- package/dist/esm/components/DatePage.js +2 -3
- package/dist/esm/components/TimeSliders.js +5 -6
- package/dist/esm/display/DateDisplay.js +7 -2
- package/dist/esm/display/DateDuration.js +127 -0
- package/dist/esm/hijri/DateConverter.js +152 -0
- package/dist/esm/hijri/FormatDate.js +112 -0
- package/dist/esm/hijri/HijriDate.js +528 -0
- package/dist/esm/hijri/StringDateParser.js +74 -0
- package/dist/esm/hijri/index.js +17 -1025
- package/dist/esm/hijri/initializer.js +113 -0
- package/dist/esm/hijri/locales.js +18 -0
- package/dist/esm/hijri/safe.js +14 -0
- package/dist/esm/i18n/ar.json.js +191 -3
- package/dist/esm/i18n/en.json.js +63 -2
- package/dist/esm/i18n/index.js +0 -6
- package/dist/esm/index.js +6 -1
- package/dist/esm/{superdate → panels}/EventPanel.js +17 -5
- package/dist/esm/utils/dateUtil.js +3 -3
- package/dist/types/display/DateDisplay.d.ts +4 -3
- package/dist/types/display/DateDuration.d.ts +15 -0
- package/dist/types/hijri/DateConverter.d.ts +13 -0
- package/dist/types/hijri/FormatDate.d.ts +19 -0
- package/dist/types/hijri/HijriDate.d.ts +214 -0
- package/dist/types/hijri/StringDateParser.d.ts +11 -0
- package/dist/types/hijri/index.d.ts +2 -2
- package/dist/types/hijri/initializer.d.ts +129 -0
- package/dist/types/hijri/locales.d.ts +19 -0
- package/dist/types/hijri/safe.d.ts +3 -0
- package/dist/types/index.d.ts +6 -1
- package/dist/types/panels/EventPanel.d.ts +17 -0
- package/dist/types/types/index.d.ts +8 -35
- package/dist/types/utils/dateUtil.d.ts +1 -1
- package/package.json +11 -11
- package/src/components/DatePage.tsx +2 -3
- package/src/components/TimeSliders.tsx +5 -6
- package/src/display/DateDisplay.tsx +15 -4
- package/src/display/DateDuration.tsx +164 -0
- package/src/hijri/DateConverter.js +175 -0
- package/src/hijri/FormatDate.js +132 -0
- package/src/hijri/HijriDate.js +477 -0
- package/src/hijri/StringDateParser.js +68 -0
- package/src/hijri/index.js +16 -1535
- package/src/hijri/initializer.js +96 -0
- package/src/hijri/locales.js +109 -0
- package/src/hijri/safe.js +16 -0
- package/src/i18n/ar.json +190 -1
- package/src/i18n/en.json +60 -0
- package/src/i18n/index.ts +0 -3
- package/src/index.ts +7 -1
- package/src/{superdate → panels}/EventPanel.tsx +41 -6
- package/src/types/index.ts +8 -38
- package/src/utils/dateUtil.ts +4 -7
- package/dist/cjs/components/Date.module.css +0 -1
- package/dist/cjs/i18n/superdate/ar.json.js +0 -86
- package/dist/cjs/i18n/superdate/en.json.js +0 -93
- package/dist/cjs/superdate/QuickSelect.js +0 -134
- package/dist/cjs/superdate/RelativeInput.js +0 -79
- package/dist/cjs/superdate/RelativePanel.js +0 -91
- package/dist/cjs/superdate/SuperDate.module.css +0 -1
- package/dist/cjs/superdate/Superdate.js +0 -169
- package/dist/esm/components/Date.module.css +0 -1
- package/dist/esm/i18n/superdate/ar.json.js +0 -79
- package/dist/esm/i18n/superdate/en.json.js +0 -85
- package/dist/esm/superdate/QuickSelect.js +0 -134
- package/dist/esm/superdate/RelativeInput.js +0 -79
- package/dist/esm/superdate/RelativePanel.js +0 -91
- package/dist/esm/superdate/SuperDate.module.css +0 -1
- package/dist/esm/superdate/Superdate.js +0 -168
- package/dist/types/superdate/EventPanel.d.ts +0 -4
- package/dist/types/superdate/QuickSelect.d.ts +0 -1
- package/dist/types/superdate/RelativeInput.d.ts +0 -1
- package/dist/types/superdate/RelativePanel.d.ts +0 -1
- package/dist/types/superdate/Superdate.d.ts +0 -17
- package/src/i18n/superdate/ar.json +0 -70
- package/src/i18n/superdate/en.json +0 -75
- package/src/superdate/QuickSelect.tsx +0 -121
- package/src/superdate/RelativeInput.tsx +0 -64
- package/src/superdate/RelativePanel.tsx +0 -103
- package/src/superdate/Superdate.tsx +0 -186
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2025 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { Dropdown, Tooltip } from "@react-fabric/core";
|
|
25
|
+
import { type MenuProps } from "@react-fabric/core/dist/types/components/menu/types";
|
|
26
|
+
import {
|
|
27
|
+
type AriaProps,
|
|
28
|
+
type ChildrenProp,
|
|
29
|
+
type CssProp,
|
|
30
|
+
type TestProps,
|
|
31
|
+
} from "@react-fabric/core/dist/types/types";
|
|
32
|
+
import { Format } from "@react-fabric/utilities";
|
|
33
|
+
import classNames from "classnames";
|
|
34
|
+
import { intervalToDuration, isBefore } from "date-fns";
|
|
35
|
+
import { useMemo, type ElementType } from "react";
|
|
36
|
+
import { useTranslation } from "react-i18next";
|
|
37
|
+
import { type DateLike } from "../types";
|
|
38
|
+
import { DateUtil } from "../utils/dateUtil";
|
|
39
|
+
|
|
40
|
+
export interface Props
|
|
41
|
+
extends CssProp,
|
|
42
|
+
AriaProps,
|
|
43
|
+
TestProps,
|
|
44
|
+
Partial<ChildrenProp<ElementType<MenuProps>>> {
|
|
45
|
+
/**
|
|
46
|
+
* date like value
|
|
47
|
+
*/
|
|
48
|
+
date: DateLike;
|
|
49
|
+
/**
|
|
50
|
+
* date format
|
|
51
|
+
*/
|
|
52
|
+
format?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const DateDuration = ({
|
|
56
|
+
date,
|
|
57
|
+
children,
|
|
58
|
+
className,
|
|
59
|
+
format = "PPpp",
|
|
60
|
+
...props
|
|
61
|
+
}: Props) => {
|
|
62
|
+
const { t } = useTranslation("date");
|
|
63
|
+
|
|
64
|
+
const interval = useMemo(() => {
|
|
65
|
+
return intervalToDuration({ start: new Date(date), end: new Date() });
|
|
66
|
+
}, [date]);
|
|
67
|
+
|
|
68
|
+
const label = useMemo(() => {
|
|
69
|
+
const before = isBefore(DateUtil.parseDate(date), new Date());
|
|
70
|
+
const tkey = before ? "relative.before" : "relative.after";
|
|
71
|
+
let part = "now";
|
|
72
|
+
let count = 0;
|
|
73
|
+
if (interval.years && interval.years > 0) {
|
|
74
|
+
part = "years";
|
|
75
|
+
count = interval.years;
|
|
76
|
+
if ((interval.months ?? 0) > 10) {
|
|
77
|
+
part = "yearsX";
|
|
78
|
+
count += 1;
|
|
79
|
+
}
|
|
80
|
+
} else if (interval.months && interval.months > 0) {
|
|
81
|
+
part = "months";
|
|
82
|
+
count = interval.months;
|
|
83
|
+
if (interval.months > 10) {
|
|
84
|
+
part = "yearsX";
|
|
85
|
+
count = 1;
|
|
86
|
+
} else if ((interval.weeks ?? 0) > 3) {
|
|
87
|
+
part = "monthsX";
|
|
88
|
+
count += 1;
|
|
89
|
+
}
|
|
90
|
+
} else if (interval.weeks && interval.weeks > 0) {
|
|
91
|
+
part = "weeks";
|
|
92
|
+
count = interval.weeks;
|
|
93
|
+
if (interval.weeks > 3) {
|
|
94
|
+
part = "monthsX";
|
|
95
|
+
count = 1;
|
|
96
|
+
} else if ((interval.days ?? 0) > 5) {
|
|
97
|
+
part = "weeksX";
|
|
98
|
+
count += 1;
|
|
99
|
+
}
|
|
100
|
+
} else if (interval.days && interval.days > 0) {
|
|
101
|
+
part = "days";
|
|
102
|
+
count = interval.days;
|
|
103
|
+
if (interval.days > 5) {
|
|
104
|
+
part = "weeksX";
|
|
105
|
+
count = 1;
|
|
106
|
+
} else if ((interval.hours ?? 0) > 20) {
|
|
107
|
+
part = "daysX";
|
|
108
|
+
count += 1;
|
|
109
|
+
}
|
|
110
|
+
} else if (interval.hours && interval.hours > 0) {
|
|
111
|
+
part = "hours";
|
|
112
|
+
count = interval.hours;
|
|
113
|
+
if (interval.hours > 20) {
|
|
114
|
+
part = "daysX";
|
|
115
|
+
count = 1;
|
|
116
|
+
} else if ((interval.minutes ?? 0) > 45) {
|
|
117
|
+
part = "hoursX";
|
|
118
|
+
count += 1;
|
|
119
|
+
}
|
|
120
|
+
} else if (interval.minutes && interval.minutes > 0) {
|
|
121
|
+
part = "minutes";
|
|
122
|
+
count = interval.minutes;
|
|
123
|
+
if (interval.minutes > 45) {
|
|
124
|
+
part = "hoursX";
|
|
125
|
+
count = 1;
|
|
126
|
+
} else if ((interval.seconds ?? 0) > 45) {
|
|
127
|
+
part = "minuteX";
|
|
128
|
+
count += 1;
|
|
129
|
+
}
|
|
130
|
+
} else if (interval.seconds && interval.seconds > 15) {
|
|
131
|
+
part = "seconds";
|
|
132
|
+
count = interval.seconds;
|
|
133
|
+
if (interval.seconds > 45) {
|
|
134
|
+
part = "minutesX";
|
|
135
|
+
count = 1;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return t(`${tkey}.${part}`, { count });
|
|
140
|
+
}, [interval, date]);
|
|
141
|
+
|
|
142
|
+
const dtLabel = (
|
|
143
|
+
<Tooltip content={Format.date(date, format)}>
|
|
144
|
+
<span
|
|
145
|
+
className={classNames(
|
|
146
|
+
className,
|
|
147
|
+
!!children && "underline decoration-dotted cursor-pointer",
|
|
148
|
+
)}
|
|
149
|
+
{...props}
|
|
150
|
+
>
|
|
151
|
+
{label}
|
|
152
|
+
</span>
|
|
153
|
+
</Tooltip>
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
return children ? (
|
|
157
|
+
<Dropdown showArrow closeOnClick>
|
|
158
|
+
{dtLabel}
|
|
159
|
+
{children}
|
|
160
|
+
</Dropdown>
|
|
161
|
+
) : (
|
|
162
|
+
dtLabel
|
|
163
|
+
);
|
|
164
|
+
};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
function intPart(floatNum) {
|
|
2
|
+
if (floatNum < -0.0000001) {
|
|
3
|
+
return Math.ceil(floatNum - 0.0000001)
|
|
4
|
+
}
|
|
5
|
+
return Math.floor(floatNum + 0.0000001)
|
|
6
|
+
}
|
|
7
|
+
var delta = 1
|
|
8
|
+
|
|
9
|
+
function GregToIsl(arg) {
|
|
10
|
+
|
|
11
|
+
let d = parseInt(arg.CDay.value)
|
|
12
|
+
let m = parseInt(arg.CMonth.value)
|
|
13
|
+
let y = parseInt(arg.CYear.value)
|
|
14
|
+
let jd, l, jd1, n, j, delta = 1;
|
|
15
|
+
if ((y > 1582) || ((y == 1582) && (m > 10)) || ((y == 1582) && (m == 10) && (d > 14))) {
|
|
16
|
+
//added delta=1 on jd to comply isna rulling 2007
|
|
17
|
+
jd = intPart((1461 * (y + 4800 + intPart((m - 14) / 12))) / 4) + intPart((367 * (m - 2 - 12 * (intPart((m - 14) / 12)))) / 12) -
|
|
18
|
+
intPart((3 * (intPart((y + 4900 + intPart((m - 14) / 12)) / 100))) / 4) + d - 32075 + delta
|
|
19
|
+
} else {
|
|
20
|
+
//added +1 on jd to comply isna rulling
|
|
21
|
+
jd = 367 * y - intPart((7 * (y + 5001 + intPart((m - 9) / 7))) / 4) + intPart((275 * m) / 9) + d + 1729777 + delta
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//added -1 on jd1 to comply isna rulling
|
|
25
|
+
jd1 = jd - delta
|
|
26
|
+
l = jd - 1948440 + 10632
|
|
27
|
+
n = intPart((l - 1) / 10631)
|
|
28
|
+
l = l - 10631 * n + 354
|
|
29
|
+
j = (intPart((10985 - l) / 5316)) * (intPart((50 * l) / 17719)) + (intPart(l / 5670)) * (intPart((43 * l) / 15238))
|
|
30
|
+
l = l - (intPart((30 - j) / 15)) * (intPart((17719 * j) / 50)) - (intPart(j / 16)) * (intPart((15238 * j) / 43)) + 29
|
|
31
|
+
m = intPart((24 * l) / 709)
|
|
32
|
+
d = l - intPart((709 * m) / 24)
|
|
33
|
+
y = 30 * n + j - 30
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
d,
|
|
37
|
+
m,
|
|
38
|
+
y,
|
|
39
|
+
dateOfWeek: jd1 % 7,
|
|
40
|
+
dayName:weekDay(jd1 % 7),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function IslToGreg(arg) {
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
let d = parseInt(arg.HDay.value)
|
|
49
|
+
let m = parseInt(arg.HMonth.value)
|
|
50
|
+
let y = parseInt(arg.HYear.value)
|
|
51
|
+
//added - delta=1 on jd to comply isna rulling
|
|
52
|
+
let jd = intPart((11 * y + 3) / 30) + 354 * y + 30 * m - intPart((m - 1) / 2) + d + 1948440 - 385 - delta
|
|
53
|
+
|
|
54
|
+
let l,n ,i ,j,k ;
|
|
55
|
+
|
|
56
|
+
if (jd > 2299160) {
|
|
57
|
+
l = jd + 68569
|
|
58
|
+
n = intPart((4 * l) / 146097)
|
|
59
|
+
l = l - intPart((146097 * n + 3) / 4)
|
|
60
|
+
i = intPart((4000 * (l + 1)) / 1461001)
|
|
61
|
+
l = l - intPart((1461 * i) / 4) + 31
|
|
62
|
+
j = intPart((80 * l) / 2447)
|
|
63
|
+
d = l - intPart((2447 * j) / 80)
|
|
64
|
+
l = intPart(j / 11)
|
|
65
|
+
m = j + 2 - 12 * l
|
|
66
|
+
y = 100 * (n - 49) + i + l
|
|
67
|
+
} else {
|
|
68
|
+
j = jd + 1402
|
|
69
|
+
k = intPart((j - 1) / 1461)
|
|
70
|
+
l = j - 1461 * k
|
|
71
|
+
n = intPart((l - 1) / 365) - intPart(l / 1461)
|
|
72
|
+
i = l - 365 * n + 30
|
|
73
|
+
j = intPart((80 * i) / 2447)
|
|
74
|
+
d = i - intPart((2447 * j) / 80)
|
|
75
|
+
i = intPart(j / 11)
|
|
76
|
+
m = j + 2 - 12 * i
|
|
77
|
+
y = 4 * k + n + i - 4716
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
//arg.CDay.value=d
|
|
81
|
+
//arg.CMonth.value=m
|
|
82
|
+
//arg.CYear.value=y
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
d,
|
|
86
|
+
m,
|
|
87
|
+
y,
|
|
88
|
+
dateOfWeek: jd % 7,
|
|
89
|
+
dayName: weekDay(jd % 7),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
export function weekDay(wdn) {
|
|
97
|
+
if (wdn == 0) {
|
|
98
|
+
return "Mon"
|
|
99
|
+
}
|
|
100
|
+
if (wdn == 1) {
|
|
101
|
+
return "Tue"
|
|
102
|
+
}
|
|
103
|
+
if (wdn == 2) {
|
|
104
|
+
return "Wed"
|
|
105
|
+
}
|
|
106
|
+
if (wdn == 3) {
|
|
107
|
+
return "Thu"
|
|
108
|
+
}
|
|
109
|
+
if (wdn == 4) {
|
|
110
|
+
return "Fri"
|
|
111
|
+
}
|
|
112
|
+
if (wdn == 5) {
|
|
113
|
+
return "Sat"
|
|
114
|
+
}
|
|
115
|
+
if (wdn == 6) {
|
|
116
|
+
return "Sun"
|
|
117
|
+
}
|
|
118
|
+
return ""
|
|
119
|
+
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function GregToHijri(gregDate) {
|
|
123
|
+
const hijriDate = GregToIsl({
|
|
124
|
+
CDay: {
|
|
125
|
+
value: gregDate.getDate()
|
|
126
|
+
},
|
|
127
|
+
CYear: {
|
|
128
|
+
value: gregDate.getFullYear()
|
|
129
|
+
},
|
|
130
|
+
CMonth: {
|
|
131
|
+
value: gregDate.getMonth() + 1
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
year: hijriDate.y,
|
|
137
|
+
month: hijriDate.m,
|
|
138
|
+
date: hijriDate.d,
|
|
139
|
+
hours: gregDate.getHours(),
|
|
140
|
+
minutes: gregDate.getMinutes(),
|
|
141
|
+
seconds: gregDate.getSeconds(),
|
|
142
|
+
milliseconds: gregDate.getMilliseconds(),
|
|
143
|
+
day: hijriDate.dateOfWeek,
|
|
144
|
+
dayName: hijriDate.dayName
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
function HijriToGreg(hijriDate) {
|
|
150
|
+
const gregDate = IslToGreg({
|
|
151
|
+
HDay: {
|
|
152
|
+
value: hijriDate._date || hijriDate.date
|
|
153
|
+
},
|
|
154
|
+
HYear: {
|
|
155
|
+
value: hijriDate._year || hijriDate.year
|
|
156
|
+
},
|
|
157
|
+
HMonth: {
|
|
158
|
+
value: hijriDate._month || hijriDate.month
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
return new Date(gregDate.y,
|
|
163
|
+
gregDate.m -1 ,
|
|
164
|
+
gregDate.d,
|
|
165
|
+
hijriDate.getHours(),
|
|
166
|
+
hijriDate.getMinutes() ,
|
|
167
|
+
hijriDate.getSeconds(),
|
|
168
|
+
hijriDate.getMilliseconds()
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export {GregToHijri};
|
|
174
|
+
|
|
175
|
+
export {HijriToGreg};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
const dateFormat = (function () {
|
|
2
|
+
const token =
|
|
3
|
+
/P{1,4}|p{1,4}|d{1,2}|e{3,6}|M{1,4}|yy(?:yy)?|([Hhms])\1?|[a]{1,4}|[LloSZ]|('at')|"[^"]*"|'[^']*'/g,
|
|
4
|
+
timezone =
|
|
5
|
+
/\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
|
|
6
|
+
timezoneClip = /[^-+\dA-Z]/g,
|
|
7
|
+
pad = function (val, len) {
|
|
8
|
+
val = String(val);
|
|
9
|
+
len = len || 2;
|
|
10
|
+
while (val.length < len) val = "0" + val;
|
|
11
|
+
return val;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// Regexes and supporting functions are cached through closure
|
|
15
|
+
return function (
|
|
16
|
+
date,
|
|
17
|
+
mask,
|
|
18
|
+
options = {
|
|
19
|
+
utc: false,
|
|
20
|
+
},
|
|
21
|
+
) {
|
|
22
|
+
const { locales, defaultLocale } = require("./HijriDate").default;
|
|
23
|
+
options.locale = options.locale || defaultLocale;
|
|
24
|
+
if (!locales[options.locale]) {
|
|
25
|
+
throw new Error(`Locale ${options.locale} is not supported yet .
|
|
26
|
+
Please, try to extend "HijriDate.locales" :
|
|
27
|
+
i.e: HijriDate.locales.${options.locale} = {dayNames:[..14 items..], monthNames:[..24 items..]}
|
|
28
|
+
`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
|
|
32
|
+
if (
|
|
33
|
+
arguments.length == 1 &&
|
|
34
|
+
Object.prototype.toString.call(date) == "[object String]" &&
|
|
35
|
+
!/\d/.test(date)
|
|
36
|
+
) {
|
|
37
|
+
mask = date;
|
|
38
|
+
date = undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
mask = String(masks[mask] || mask || masks["default"]);
|
|
42
|
+
|
|
43
|
+
// Allow setting the utc argument via the mask
|
|
44
|
+
if (mask.slice(0, 4) == "UTC:") {
|
|
45
|
+
mask = mask.slice(4);
|
|
46
|
+
options.utc = true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var _ = options.utc ? "getUTC" : "get",
|
|
50
|
+
d = date[_ + "Date"](),
|
|
51
|
+
D = date[_ + "Day"](),
|
|
52
|
+
M = date[_ + "Month"](),
|
|
53
|
+
mIndex = date[_ + "MonthIndex"](),
|
|
54
|
+
y = date[_ + "FullYear"](),
|
|
55
|
+
H = date[_ + "Hours"](),
|
|
56
|
+
m = date[_ + "Minutes"](),
|
|
57
|
+
s = date[_ + "Seconds"](),
|
|
58
|
+
L = date[_ + "Milliseconds"](),
|
|
59
|
+
o = options.utc ? 0 : date.getTimezoneOffset(),
|
|
60
|
+
flags = {
|
|
61
|
+
d: d,
|
|
62
|
+
dd: pad(d),
|
|
63
|
+
ddd: locales[options.locale].dayNames[D === 7 ? 0 : D + 1],
|
|
64
|
+
dddd: locales[options.locale].dayNames[(D === 7 ? 0 : D + 1) + 7],
|
|
65
|
+
ee: locales[options.locale].dayNames[D],
|
|
66
|
+
eee: locales[options.locale].dayNames[D + 7],
|
|
67
|
+
eeee: locales[options.locale].dayNames[D + 14],
|
|
68
|
+
eeeeee: locales[options.locale].dayNames[D],
|
|
69
|
+
M,
|
|
70
|
+
MM: pad(M),
|
|
71
|
+
MMM: locales[options.locale].monthNames[mIndex],
|
|
72
|
+
MMMM: locales[options.locale].monthNames[mIndex + 12],
|
|
73
|
+
yy: String(y).slice(2),
|
|
74
|
+
yyyy: y,
|
|
75
|
+
h: H % 12 || 12,
|
|
76
|
+
hh: pad(H % 12 || 12),
|
|
77
|
+
H: H,
|
|
78
|
+
HH: pad(H),
|
|
79
|
+
m,
|
|
80
|
+
mm: pad(m),
|
|
81
|
+
s: s,
|
|
82
|
+
ss: pad(s),
|
|
83
|
+
l: pad(L, 3),
|
|
84
|
+
L: pad(L > 99 ? Math.round(L / 10) : L),
|
|
85
|
+
a: H < 12 ? locales[options.locale].am : locales[options.locale].pm,
|
|
86
|
+
aa: H < 12 ? locales[options.locale].am : locales[options.locale].pm,
|
|
87
|
+
aaa: H < 12 ? locales[options.locale].am : locales[options.locale].pm,
|
|
88
|
+
aaaa: H < 12 ? locales[options.locale].am : locales[options.locale].pm,
|
|
89
|
+
aaaaa: H < 12 ? locales[options.locale].am : locales[options.locale].pm,
|
|
90
|
+
"'at'": locales[options.locale].separator,
|
|
91
|
+
t: H < 12 ? "a" : "p",
|
|
92
|
+
tt: H < 12 ? "am" : "pm",
|
|
93
|
+
T: H < 12 ? "A" : "P",
|
|
94
|
+
TT: H < 12 ? "AM" : "PM",
|
|
95
|
+
// Z: options.utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
|
|
96
|
+
o:
|
|
97
|
+
(o > 0 ? "-" : "+") +
|
|
98
|
+
pad(Math.floor(Math.abs(o) / 60) * 100 + (Math.abs(o) % 60), 4),
|
|
99
|
+
S: ["th", "st", "nd", "rd"][
|
|
100
|
+
d % 10 > 3 ? 0 : (((d % 100) - (d % 10) != 10) * d) % 10
|
|
101
|
+
],
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
return mask.replace(token, function ($0) {
|
|
105
|
+
if ($0 === "P") return `${flags.d}/${flags.M}/${flags.yyyy} `;
|
|
106
|
+
if ($0.startsWith("PP"))
|
|
107
|
+
return `${flags.MMM} ${flags.dd}, ${flags.yyyy} `;
|
|
108
|
+
if ($0 === "p") return `${flags.h}:${flags.m} ${flags.a}`;
|
|
109
|
+
if ($0.startsWith("pp"))
|
|
110
|
+
return `${flags.hh}:${flags.mm}:${flags.ss} ${flags.a}`;
|
|
111
|
+
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
})();
|
|
115
|
+
|
|
116
|
+
// Some common format strings
|
|
117
|
+
export const masks = {
|
|
118
|
+
default: "ddd MMM dd yyyy HH:mm:ss",
|
|
119
|
+
shortDate: "M/d/yy",
|
|
120
|
+
mediumDate: "MMM d, yyyy",
|
|
121
|
+
longDate: "MMMM d, yyyy",
|
|
122
|
+
fullDate: "dddd, MMMM d, yyyy",
|
|
123
|
+
shortTime: "h:mm TT",
|
|
124
|
+
mediumTime: "h:mm:ss TT",
|
|
125
|
+
longTime: "h:mm:ss TT Z",
|
|
126
|
+
isoDate: "yyyy-MM-dd",
|
|
127
|
+
isoTime: "HH:mm:ss",
|
|
128
|
+
isoDateTime: "yyyy-MM-dd'T'HH:mm:ss",
|
|
129
|
+
isoUtcDateTime: "UTC:yyyy-MM-dd'T'HH:mm:ss'Z'",
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export default dateFormat;
|