dayjs
Version:
2KB immutable date time library alternative to Moment.js with the same modern API
76 lines (62 loc) • 1.55 kB
JavaScript
export default (function (option, Dayjs, dayjs) {
var proto = Dayjs.prototype;
dayjs.utc = function (date, format) {
var cfg = {
date: date,
utc: true,
format: format
};
return new Dayjs(cfg); // eslint-disable-line no-use-before-define
};
proto.utc = function () {
return dayjs(this.toDate(), {
locale: this.$L,
utc: true
});
};
proto.local = function () {
return dayjs(this.toDate(), {
locale: this.$L,
utc: false
});
};
var oldParse = proto.parse;
proto.parse = function (cfg) {
if (cfg.utc) {
this.$u = true;
}
oldParse.call(this, cfg);
};
var oldInit = proto.init;
proto.init = function () {
if (this.$u) {
var $d = this.$d;
this.$y = $d.getUTCFullYear();
this.$M = $d.getUTCMonth();
this.$D = $d.getUTCDate();
this.$W = $d.getUTCDay();
this.$H = $d.getUTCHours();
this.$m = $d.getUTCMinutes();
this.$s = $d.getUTCSeconds();
this.$ms = $d.getUTCMilliseconds();
} else {
oldInit.call(this);
}
};
var oldUtcOffset = proto.utcOffset;
proto.utcOffset = function () {
if (this.$u) {
return 0;
}
return oldUtcOffset.call(this);
};
var oldFormat = proto.format;
var UTC_FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ss[Z]';
proto.format = function (formatStr) {
var str = formatStr || (this.$u ? UTC_FORMAT_DEFAULT : '');
return oldFormat.call(this, str);
};
proto.isUTC = function () {
return !!this.$u;
};
});