-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
133 lines (111 loc) · 3.17 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const { startOfDay, startOfMonth, endOfDay } = require('date-fns');
import formatBase from 'zent/es/utils/date/formatDate';
import parseBase from 'zent/es/utils/date/parseDate';
import { getLocale } from '../../i18n/time-locale';
import { CURRENT_MONTH, ONEDAY, TIME_BEGIN } from '../constants';
export const padLeft = val => {
return val < 10 ? `0${val}` : val;
};
export const getMonthStr = val => {
return `${val.getFullYear()}-${padLeft(val.getMonth() + 1)}`;
};
export const getDateStr = val => {
return `${val.getFullYear()}-${padLeft(val.getMonth() + 1)}-${val.getDate()}`;
};
export const isSameDate = (val, cmp) => {
return (
val.getFullYear() === cmp.getFullYear() &&
val.getMonth() === cmp.getMonth() &&
val.getDate() === cmp.getDate()
);
};
export const isCurrentMonth = val => {
return val.getMonth() === CURRENT_MONTH;
};
export const isBeforeMonth = (val, cpr) => {
if (val.getFullYear() < cpr.getFullYear()) {
return true;
}
return (
val.getFullYear() === cpr.getFullYear() && val.getMonth() < cpr.getMonth()
);
};
export const isAfterMonth = (val, cpr) => {
if (val.getFullYear() > cpr.getFullYear()) {
return true;
}
return (
val.getFullYear() === cpr.getFullYear() && val.getMonth() > cpr.getMonth()
);
};
export const goDays = (val, diff) => {
return new Date(val.getTime() + diff * ONEDAY);
};
export const goMonths = (val, diff) => {
const cp = new Date(val);
return new Date(cp.getFullYear(), cp.getMonth() + diff, 1);
};
export const goYears = (val, diff) => {
const cp = new Date(val);
return new Date(cp.setFullYear(cp.getFullYear() + diff));
};
export const cloneFromDate = target => {
return new Date(target.getFullYear(), target.getMonth(), target.getDate());
};
/**
* add by fancy to inject i18n
* simple wrapper for formatDate in zan-utils
*
* @param {Date|number} date The date to format
* @param {string} format
* @param {string|object} locale the i18n setting for fecha
* @returns {strning} format result by zan-utils
*/
export function formatDate(date, format, locale = getLocale()) {
return formatBase(date, format, locale);
}
/**
* add by fancy to inject i18n
* simple wrapper for parseDate in zan-utils
*
* @param {string} dateStr Date string to parse
* @param {string} format
* @param {string|object} locale the i18n setting for fecha
*/
export function parseDate(dateStr, format, locale = getLocale()) {
return parseBase(dateStr, format, locale);
}
export function monthStart(date = new Date()) {
return startOfMonth(date);
}
export function dayStart(date = new Date()) {
return startOfDay(date);
}
export function dayEnd(date = new Date()) {
return endOfDay(date);
}
export function setTime(date: Date, time: Date | string = TIME_BEGIN): Date {
let timeArr;
if (time instanceof Date) {
timeArr = [time.getHours(), time.getMinutes(), time.getSeconds()];
} else {
timeArr = time.split(':');
}
const dateTimeArr: any = [
date.getFullYear(),
date.getMonth(),
date.getDate(),
...timeArr,
];
return new (Date as any)(...dateTimeArr);
}
export const commonFns = {
goDays,
goMonths,
goYears,
setTime,
dayStart,
dayEnd,
parseDate,
formatDate,
};