lavkach3

Форк
0
409 строк · 14.0 Кб
1
(function(global, factory) {
2
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
        typeof define === 'function' && define.amd ? define(factory) :
4
        global.moment = factory();
5
}(this, function() {
6
    "use strict";
7
    const FORMAT_LIST = {
8
        "l": "YYYY-MM-DD",
9
        "ll": "YYYY年MM月DD日",
10
        "k": "YYYY-MM-DD hh:mm",
11
        "kk": "YYYY年MM月DD日 hh点mm分",
12
        "kkk": "YYYY年MM月DD日 hh点mm分 q",
13
        "f": "YYYY-MM-DD hh:mm:ss",
14
        "ff": "YYYY年MM月DD日 hh点mm分ss秒",
15
        "fff": "YYYY年MM月DD日 hh点mm分ss秒 星期w",
16
        "n": "MM-DD",
17
        "nn": "MM月DD日",
18
    }
19

20
    const _SECONDS = 1000;
21
    const _MINUTES = 1000 * 60;
22
    const _HOURS = 1000 * 60 * 60;
23
    const _DAYS = 1000 * 60 * 60 * 24;
24
    const _WEEKS = _DAYS * 7;
25
    const _YEARS = _DAYS * 365;
26
    const MSE = new Date(1970, 0, 1, 0, 0, 0).getTime();
27

28
    const WEEK = ['日', '一', '二', '三', '四', '五', '六'];
29
    const DAY_STRING = ['上午', '下午'];
30
    let _moment = function() {
31
        Utils.initMoment(this, ...arguments);
32
    };
33

34
    let Utils = {
35
        initMoment(moment_obj, arg_1, type) {
36
            let _date = new Date(),date_bak = _date;
37
            if (arg_1 != undefined) {
38
                if (Utils.isNumber(arg_1)) {
39
                    if (arg_1 < 9999999999) arg_1 = arg_1 * 1000;
40
                    _date.setTime(arg_1);
41
                } else if (Utils.isArray(arg_1)) {
42
                    Utils.padMonth(arg_1);
43
                    _date = new Date(...arg_1);
44
                } else if (Utils.isDate(arg_1)) {
45
                    _date = arg_1;
46
                } else if (Utils.isString(arg_1)) {
47
                    _date = Utils.parse(arg_1);
48
                } else if (arg_1 instanceof _moment) {
49
                    return arg_1;
50
                }
51
            }
52
            moment_obj._date = _date;
53
            if(date_bak === _date&&moment_obj.timeDelay!=0){
54
                moment_obj.add(moment_obj.timeDelay,moment.TIME);
55
            }
56
        },
57
        parse(str) {
58
            let aspNetJsonRegex = /^(\d{4})\-?(\d{2})\-?(\d{2})\s?\:?(\d{2})?\:?(\d{2})?\:?(\d{2})?$/i;
59
            var matched = aspNetJsonRegex.exec(str);
60
            if (matched !== null) {
61
                matched.shift();
62
                Utils.padMonth(matched);
63
                Utils.popUndefined(matched);
64
                return new Date(...matched);
65
            }
66
            let date = new Date(str);
67
            if(date=="Invalid Date"){
68
                console.error("Invalid date parse from \""+str+"\"");
69
                return null;
70
            }else{
71
                return date;
72
            }
73
        },
74
        popUndefined(arr) {
75
            if (arr.length > 0 && arr[arr.length - 1] == undefined) {
76
                arr.pop();
77
                return Utils.popUndefined(arr);
78
            }
79
            return arr;
80
        },
81
        padMonth(arr) {
82
            //自动补充月份
83
            if (arr.length > 1 && arr[1] > 0) arr[1] -= 1;
84
        },
85
        isLeapYear(year) {
86
            return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
87
        },
88
        format(date, formatStr) {
89
            let str = formatStr;
90
            str = str.replace(/yyyy|YYYY/, date.getFullYear());
91
            str = str.replace(/yy|YY/, (date.getYear() % 100) > 8 ? (date.getYear() % 100).toString() : '0' + (date.getYear() % 100));
92
            str = str.replace(/MM/, date.getMonth() > 8 ? (date.getMonth() + 1).toString() : ('0' + (date.getMonth() + 1)));
93
            str = str.replace(/M/g, (date.getMonth() + 1));
94
            str = str.replace(/w|W/g, WEEK[date.getDay()]);
95
            str = str.replace(/dd|DD/, date.getDate() > 9 ? date.getDate().toString() : '0' + date.getDate());
96
            str = str.replace(/d|D/g, date.getDate());
97
            str = str.replace(/hh|HH/, date.getHours() > 9 ? date.getHours().toString() : '0' + date.getHours());
98
            str = str.replace(/h|H/g, date.getHours());
99
            str = str.replace(/mm/, date.getMinutes() > 9 ? date.getMinutes().toString() : '0' + date.getMinutes());
100
            str = str.replace(/m/g, date.getMinutes());
101
            str = str.replace(/ss|SS/, date.getSeconds() > 9 ? date.getSeconds().toString() : '0' + date.getSeconds());
102
            str = str.replace(/s|S/g, date.getSeconds());
103
            str = str.replace(/q|Q/g, date.getHours() > 12 ? DAY_STRING[1] : DAY_STRING[0]);
104
            return str;
105
        },
106
        timestamp(date) {
107
            return Math.floor(date.getTime() / 1000);
108
        },
109
        getDays(date) {
110
            return Math.floor((date.getTime() - MSE) / _DAYS);
111
        },
112
        getHours(date) {
113
            return Math.floor((date.getTime() - MSE) / _HOURS);
114
        },
115
        getMonths(date) {
116
            return date.getYear() * 12 + date.getMonth() + 1;
117
        },
118
        isObject(input) {
119
            return Object.prototype.toString.call(input) === '[object Object]';
120
        },
121
        isArray(input) {
122
            return input instanceof Array || Object.prototype.toString.call(input) === '[object Array]';
123
        },
124
        isDate(input) {
125
            return input instanceof Date || Object.prototype.toString.call(input) === '[object Date]';
126
        },
127
        isNumber(input) {
128
            return input instanceof Number || Object.prototype.toString.call(input) === '[object Number]';
129
        },
130
        isString(input) {
131
            return input instanceof String || Object.prototype.toString.call(input) === '[object String]';
132
        },
133
        extend(a, b) {
134
            for (var i in b) {
135
                if (hasOwnProp(b, i)) {
136
                    a[i] = b[i];
137
                }
138
            }
139

140
            if (hasOwnProp(b, 'toString')) {
141
                a.toString = b.toString;
142
            }
143

144
            if (hasOwnProp(b, 'valueOf')) {
145
                a.valueOf = b.valueOf;
146
            }
147

148
            return a;
149
        },
150
        makeGetSet(unit) {
151
            return function(value) {
152
                if (value != undefined) {
153
                    // if(unit=="Month")value = value>0?(value-1):0;
154
                    Date.prototype["set" + unit].call(this._date, value);
155
                    return this;
156
                } else {
157
                    return Date.prototype["get" + unit].call(this._date);
158
                    // return unit=="Month"?(result+1):result;
159
                }
160
            };
161
        }
162
    }
163

164

165
    function hasOwnProp(a, b) {
166
        return Object.prototype.hasOwnProperty.call(a, b);
167
    }
168

169

170
    _moment.prototype = {
171
        timeDelay:0,
172
        format(str) {
173
            let m = this;
174

175
            let v = this.isValid();
176
            if(v!==true)return v;
177

178
            str = str || "l";
179
            let formatStr = FORMAT_LIST[str] || str;
180
            return Utils.format(m._date, formatStr);
181
        },
182
        toString() {
183
            let v = this.isValid();
184
            if(v!==true)return v;
185
            return this._date.toString();
186
        },
187
        toISOString() {
188
            let v = this.isValid();
189
            if(v!==true)return v;
190
            return this._date.toISOString();
191
        },
192
        distance(_m, type) {
193
            let v = this.isValid();
194
            if(v!==true)return v;
195
            let m = this;
196
            type = type || moment.DAY;
197
            _m = moment(_m);
198
            v = _m.isValid();
199
            if(v!==true)return v;
200
            switch (type) {
201
                case moment.HOUR:
202
                    return Utils.getHours(m._date) - Utils.getHours(_m._date);
203
                case moment.DAY:
204
                    return Utils.getDays(m._date) - Utils.getDays(_m._date);
205
                case moment.MONTH:
206
                    return Utils.getMonths(m._date) - Utils.getMonths(_m._date);
207
                case moment.YEAR:
208
                    return m._date.getYear() - _m._date.getYear();
209
            }
210
            return 0;
211
        },
212
        getWeekOfYear(weekStart){
213
            let diff = 0;
214
            if(weekStart&&weekStart==moment.MONDAY){
215
                diff=1;
216
            }
217
            let _date = this._date;
218
            let year = _date.getFullYear();  
219
            let firstDay = new Date(year, 0, 1);  
220
            let firstWeekDays = 7 - firstDay.getDay() + diff;  
221
            let dayOfYear = (((new Date(year, _date.getMonth(), _date.getDate())) - firstDay) / (24 * 3600 * 1000)) + 1; 
222
            return Math.ceil((dayOfYear - firstWeekDays) / 7) + 1;  
223
        },
224
        getWeekOfMonth(weekStart) {  
225
            let diff = 0;
226
            if(weekStart&&weekStart==moment.MONDAY){
227
                diff=1;
228
            }
229
            var dayOfWeek = this.day();  
230
            var day = this.date();  
231
            return Math.ceil((day - dayOfWeek - 1) / 7) + ((dayOfWeek >= weekStart) ? 1 : 0);  
232
        },
233
        isLeapYear() {
234
            let v = this.isValid();
235
            if(v!==true)return v;
236
            return Utils.isLeapYear(this.year());
237
        },
238
        isThisYear() {
239
            let v = this.isValid();
240
            if(v!==true)return v;
241
            return Utils.timestamp(this._date);
242
        },
243
        isBefore() {
244
            let v = this.isValid();
245
            if(v!==true)return v;
246
            return Utils.timestamp(this._date);
247
        },
248
        isAfter() {
249
            let v = this.isValid();
250
            if(v!==true)return v;
251
            return Utils.timestamp(this._date);
252
        },
253
        month(num) {
254
            let v = this.isValid();
255
            if(v!==true)return v;
256
            let m = this;
257
            if (num == undefined) {
258
                return m._date.getMonth() + 1;
259
            }
260
            num = parseInt(num);
261
            num = m._date.setMonth(num - 1);
262
            return m;
263
        },
264
        add(num, type) {
265
            let v = this.isValid();
266
            if(v!==true)return v;
267
            let m = this;
268
            num = parseInt(num);
269
            type = type || moment.DAY;
270

271
            switch (type) {
272
                case moment.DAY:
273
                    m.time(m.time() + (num * _DAYS));
274
                    break;
275
                case moment.MONTH:
276
                    let month_add = m.month() + num;
277
                    let year_add = Math.floor(month_add / 12);
278
                    month_add = month_add % 12;
279
                    m.add(year_add, moment.YEAR);
280
                    m.month(month_add);
281
                    break;
282
                case moment.YEAR:
283
                    m.year(m.year() + num);
284
                    break;
285
                case moment.WEEK:
286
                    m.time(m.time() + (num * _WEEKS));
287
                    break;
288
                case moment.HOUR:
289
                    m.time(m.time() + (num * _HOURS));
290
                    break;
291
                case moment.MINUTE:
292
                    m.time(m.time() + (num * _MINUTES));
293
                    break;
294
                case moment.SECOND:
295
                    m.time(m.time() + (num * _SECONDS));
296
                    break;
297
                case moment.TIME:
298
                    m.time(m.time() + (num));
299
                    break;
300
            }
301
            return m;
302
        },
303
        endOf(type,set) {
304
            let v = this.isValid();
305
            if(v!==true)return v;
306
            let m = this;
307
            type = type || moment.DAY;
308
            m.startOf(type,set);
309
            m.add(1, type);
310
            // if (moment.DAY == type||moment.WEEK == type) {
311
                m.add(-1, moment.SECOND);
312
            // } else {
313
                // m.add(-1, moment.DAY);
314
            // }
315
            return m;
316
        },
317
        startOf(type,set) {
318
            let v = this.isValid();
319
            if(v!==true)return v;
320
            let m = this;
321
            type = type || moment.DAY;
322
            switch (type) {
323
                case moment.DAY:
324
                    m.milliseconds(0);
325
                    m.seconds(0);
326
                    m.minutes(0);
327
                    m.hours(0);
328
                    break;
329
                case moment.MONTH:
330
                    m.date(1);
331
                    m.startOf(moment.DAY);
332
                    break;
333
                case moment.WEEK:
334
                    m.startOf(moment.DAY);
335
                    set=set||moment.SUNDAY;
336
                    let startDay = set==moment.SUNDAY?0:1;
337
                    m.add(-m.day()+startDay,moment.DAY);
338
                    break;
339
                case moment.YEAR:
340
                    m.month(1);
341
                    m.date(1);
342
                    m.startOf(moment.DAY);
343
                    break;
344
                case moment.HOUR:
345
                    m.time(Math.floor((m.time()) / _HOURS) * _HOURS);
346
                    break;
347
            }
348
            return m;
349
        },
350
        isValid(){
351
            return Utils.isDate(this._date)?true:"Invalid Date";
352
        }
353
    };
354

355
    let momentPrototype__proto = _moment.prototype;
356

357
    const methods = {
358
        "year": "FullYear",
359
        "day": "Day",
360
        "date": "Date",
361
        "hours": "Hours",
362
        "milliseconds": "Milliseconds",
363
        "seconds": "Seconds",
364
        "minutes": "Minutes",
365
        "time": "Time",
366
    };
367

368
    for (let unit in methods) {
369
        momentPrototype__proto[unit] = Utils.makeGetSet(methods[unit]);
370
    }
371

372
    let moment = function(param) {
373
        if(param instanceof _moment){
374
            return param;
375
        }else if (Utils.isObject(param)) {
376
            //config
377
            if (param.formatString && Utils.isObject(param.formatString)) {
378
                Utils.extend(FORMAT_LIST, param.formatString);
379
            }
380
            if (param.now) {
381
                _moment.prototype.timeDelay = moment(param.now).time() - moment().time();
382
            }
383
        } else {
384
            return new _moment(param);
385
        }
386
    };
387

388
    moment.config = function(param) {
389
        if (param.formatString && Utils.isObject(param.formatString)) {
390
            Utils.extend(FORMAT_LIST, param.formatString);
391
        }
392
        if (param.now) {
393
            _moment.prototype.timeDelay = moment(param.now).time() - moment().time();
394
        }
395
    };
396

397
    moment.SECOND = 2;
398
    moment.MINUTE = 3;
399
    moment.HOUR = 4;
400
    moment.DAY = 5;
401
    moment.MONTH = 6;
402
    moment.YEAR = 7;
403
    moment.WEEK = 8;
404
    moment.TIME = 9;
405

406
    moment.MONDAY = 1;
407
    moment.SUNDAY = 2;
408
    return moment;
409
}));
410

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.