@autobusal/utilities 0.0.2 → 0.0.4
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/date.tsx +46 -0
- package/index.ts +8 -2
- package/package.json +1 -1
package/date.tsx
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { TFunction } from 'i18next';
|
|
2
|
+
|
|
3
|
+
export const createDate = (prepared: string): Date => {
|
|
4
|
+
const data = prepared.split('/');
|
|
5
|
+
|
|
6
|
+
return new Date(Date.UTC(Number(data[2]), Number(data[1]) - 1, Number(data[0])));
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const prepareDate = (date: Date): string => {
|
|
10
|
+
const day = date.getDate();
|
|
11
|
+
const month = date.getMonth() + 1;
|
|
12
|
+
|
|
13
|
+
return `${ day < 10 ? `0${ day }` : day }/${ month < 10 ? `0${ month }` : month }/${ date.getFullYear() }`;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const getFormattedShort = (date: Date, t: TFunction<'generate'>): string => {
|
|
17
|
+
const day = date.getDate();
|
|
18
|
+
const month = date.toLocaleString('default', { month: 'long' });
|
|
19
|
+
const weekday = date.toLocaleString('default', { weekday: 'short' });
|
|
20
|
+
|
|
21
|
+
const display = {
|
|
22
|
+
weekday: `general:date.days.${ weekday.toLowerCase() }`,
|
|
23
|
+
day: day < 10 ? `0${ day }` : day,
|
|
24
|
+
month: `general:date.months.${ month.toLowerCase() }`
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return `${ t(display.weekday) }, ${ display.day } ${ t(display.month).substring(0, 3) }`;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const differenceYears = (date: Date): number => {
|
|
31
|
+
const milliseconds = Date.now() - date.getTime();
|
|
32
|
+
|
|
33
|
+
const difference = new Date(milliseconds);
|
|
34
|
+
|
|
35
|
+
// calculate the miliseconds from epoch
|
|
36
|
+
return Math.abs(difference.getUTCFullYear() - 1970);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const futureDate = (days: number): string => {
|
|
40
|
+
const today = new Date();
|
|
41
|
+
|
|
42
|
+
const dayAfter = new Date(today);
|
|
43
|
+
dayAfter.setDate(today.getDate() + days);
|
|
44
|
+
|
|
45
|
+
return prepareDate(dayAfter);
|
|
46
|
+
};
|
package/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { createDate, prepareDate, getFormattedShort, differenceYears, futureDate } from './date';
|
|
1
2
|
import { validate, display } from './form';
|
|
2
|
-
import { error, notify, success } from './notify';
|
|
3
3
|
import getPosition from './map';
|
|
4
|
+
import { error, notify, success } from './notify';
|
|
4
5
|
|
|
5
6
|
export {
|
|
6
7
|
validate,
|
|
@@ -8,5 +9,10 @@ export {
|
|
|
8
9
|
error,
|
|
9
10
|
notify,
|
|
10
11
|
success,
|
|
11
|
-
getPosition
|
|
12
|
+
getPosition,
|
|
13
|
+
createDate,
|
|
14
|
+
prepareDate,
|
|
15
|
+
getFormattedShort,
|
|
16
|
+
differenceYears,
|
|
17
|
+
futureDate
|
|
12
18
|
};
|