moment.js
4.68 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _moment = _interopRequireDefault(require("moment"));
var _warning = require("rc-util/lib/warning");
var generateConfig = {
// get
getNow: function getNow() {
return (0, _moment.default)();
},
getFixedDate: function getFixedDate(string) {
return (0, _moment.default)(string, 'YYYY-MM-DD');
},
getEndDate: function getEndDate(date) {
var clone = date.clone();
return clone.endOf('month');
},
getWeekDay: function getWeekDay(date) {
var clone = date.clone().locale('en_US');
return clone.weekday() + clone.localeData().firstDayOfWeek();
},
getYear: function getYear(date) {
return date.year();
},
getMonth: function getMonth(date) {
return date.month();
},
getDate: function getDate(date) {
return date.date();
},
getHour: function getHour(date) {
return date.hour();
},
getMinute: function getMinute(date) {
return date.minute();
},
getSecond: function getSecond(date) {
return date.second();
},
// set
addYear: function addYear(date, diff) {
var clone = date.clone();
return clone.add(diff, 'year');
},
addMonth: function addMonth(date, diff) {
var clone = date.clone();
return clone.add(diff, 'month');
},
addDate: function addDate(date, diff) {
var clone = date.clone();
return clone.add(diff, 'day');
},
setYear: function setYear(date, year) {
var clone = date.clone();
return clone.year(year);
},
setMonth: function setMonth(date, month) {
var clone = date.clone();
return clone.month(month);
},
setDate: function setDate(date, num) {
var clone = date.clone();
return clone.date(num);
},
setHour: function setHour(date, hour) {
var clone = date.clone();
return clone.hour(hour);
},
setMinute: function setMinute(date, minute) {
var clone = date.clone();
return clone.minute(minute);
},
setSecond: function setSecond(date, second) {
var clone = date.clone();
return clone.second(second);
},
// Compare
isAfter: function isAfter(date1, date2) {
return date1.isAfter(date2);
},
isValidate: function isValidate(date) {
return date.isValid();
},
locale: {
getWeekFirstDay: function getWeekFirstDay(locale) {
var date = (0, _moment.default)().locale(locale);
return date.localeData().firstDayOfWeek();
},
getWeekFirstDate: function getWeekFirstDate(locale, date) {
var clone = date.clone();
var result = clone.locale(locale);
return result.weekday(0);
},
getWeek: function getWeek(locale, date) {
var clone = date.clone();
var result = clone.locale(locale);
return result.week();
},
getShortWeekDays: function getShortWeekDays(locale) {
var date = (0, _moment.default)().locale(locale);
return date.localeData().weekdaysMin();
},
getShortMonths: function getShortMonths(locale) {
var date = (0, _moment.default)().locale(locale);
return date.localeData().monthsShort();
},
format: function format(locale, date, _format) {
var clone = date.clone();
var result = clone.locale(locale);
return result.format(_format);
},
parse: function parse(locale, text, formats) {
var fallbackFormatList = [];
for (var i = 0; i < formats.length; i += 1) {
var format = formats[i];
var formatText = text;
if (format.includes('wo') || format.includes('Wo')) {
format = format.replace(/wo/g, 'w').replace(/Wo/g, 'W');
var matchFormat = format.match(/[-YyMmDdHhSsWwGg]+/g);
var matchText = formatText.match(/[-\d]+/g);
if (matchFormat && matchText) {
format = matchFormat.join('');
formatText = matchText.join('');
} else {
fallbackFormatList.push(format.replace(/o/g, ''));
}
}
var date = (0, _moment.default)(formatText, format, locale, true);
if (date.isValid()) {
return date;
}
} // Fallback to fuzzy matching, this should always not reach match or need fire a issue
for (var _i = 0; _i < fallbackFormatList.length; _i += 1) {
var _date = (0, _moment.default)(text, fallbackFormatList[_i], locale, false);
/* istanbul ignore next */
if (_date.isValid()) {
(0, _warning.noteOnce)(false, 'Not match any format strictly and fallback to fuzzy match. Please help to fire a issue about this.');
return _date;
}
}
return null;
}
}
};
var _default = generateConfig;
exports.default = _default;