UNPKG

java-localdatetime

Version:

### 日期工具库

218 lines (217 loc) 9.44 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalDateTime = void 0; var moment = require("moment"); var abstractDate_1 = require("./abstractDate"); var consts_1 = require("./consts"); var localDate_1 = require("./localDate"); var localTime_1 = require("./localTime"); var LocalDateTime = /** @class */ (function (_super) { __extends(LocalDateTime, _super); function LocalDateTime(date) { var _this = _super.call(this) || this; _this.momentDate = date ? moment.utc(date, consts_1.DATE_TIME_FORMAT) : moment(); return _this; } LocalDateTime.of = function (year, month, date, hour, minute, second) { if (hour === void 0) { hour = 0; } if (minute === void 0) { minute = 0; } if (second === void 0) { second = 0; } var localDate = localDate_1.LocalDate.of(year, month, date); var localTime = localTime_1.LocalTime.of(hour, minute, second); return new LocalDateTime(localDate.format() + ' ' + localTime.format()); }; LocalDateTime.now = function () { var _a = _super.buildDateProperty.call(this), year = _a.year, month = _a.month, date = _a.date, hour = _a.hour, minute = _a.minute, second = _a.second; return LocalDateTime.of(year, month, date, hour, minute, second); }; LocalDateTime.parse = function (date) { return new LocalDateTime(date); }; LocalDateTime.parseMoment = function (moment) { var _a = _super.buildDateProperty.call(this, moment), year = _a.year, month = _a.month, date = _a.date, hour = _a.hour, minute = _a.minute, second = _a.second; return LocalDateTime.of(year, month, date, hour, minute, second); }; LocalDateTime.prototype.clone = function () { return LocalDateTime.parseMoment(this.momentDate.clone()); }; LocalDateTime.prototype.isSameDay = function (date) { var dateMoment = localDate_1.LocalDate.of(date.getYear(), date.getMonth(), date.getDate()).toMoment(); var thisMomentDay = localDate_1.LocalDate.of(this.getYear(), this.getMonth(), this.getDate()).toMoment(); return dateMoment.isSame(thisMomentDay); }; LocalDateTime.prototype.isSameWeek = function (date) { var endOfWeek = date.atEndOfWeek(); var currentWeek = this.atEndOfWeek(); var dateMoment = localDate_1.LocalDate.of(endOfWeek.getYear(), endOfWeek.getMonth(), endOfWeek.getDate()).toMoment(); var thisMomentWeek = localDate_1.LocalDate.of(currentWeek.getYear(), currentWeek.getMonth(), currentWeek.getDate()).toMoment(); return dateMoment.isSame(thisMomentWeek); }; LocalDateTime.prototype.isSameMonth = function (date) { var dateMoment = localDate_1.LocalDate.of(date.getYear(), date.getMonth(), 1).toMoment(); var thisMomentMonth = localDate_1.LocalDate.of(this.getYear(), this.getMonth(), 1).toMoment(); return dateMoment.isSame(thisMomentMonth); }; LocalDateTime.prototype.isBefore = function (date) { return this.momentDate.isBefore(date.momentDate); }; LocalDateTime.prototype.isAfter = function (date) { return this.momentDate.isAfter(date.momentDate); }; LocalDateTime.prototype.getYear = function () { return this.year(); }; LocalDateTime.prototype.getMonth = function () { return this.month(); }; LocalDateTime.prototype.getDate = function () { return this.date(); }; LocalDateTime.prototype.getHour = function () { return this.hour(); }; LocalDateTime.prototype.getMinute = function () { return this.minute(); }; LocalDateTime.prototype.getSecond = function () { return this.second(); }; LocalDateTime.prototype.getWeek = function () { return this.week(); }; LocalDateTime.prototype.weekday = function (num) { this.momentDate.weekday(num); return this; }; LocalDateTime.prototype.getWeekDay = function () { return this.weekDay(); }; LocalDateTime.prototype.getTime = function () { return this.momentDate.valueOf(); }; LocalDateTime.prototype.plusYear = function (year) { return LocalDateTime.parseMoment(this.clone().momentDate.add(year, 'y')); }; LocalDateTime.prototype.minusYear = function (year) { return this.plusYear(year * -1); }; LocalDateTime.prototype.plusMonth = function (month) { return LocalDateTime.parseMoment(this.clone().momentDate.add(month, 'M')); }; LocalDateTime.prototype.minusMonth = function (month) { return this.plusMonth(month * -1); }; LocalDateTime.prototype.plusDay = function (day) { return LocalDateTime.parseMoment(this.clone().momentDate.add(day, 'd')); }; LocalDateTime.prototype.minusDay = function (day) { return this.plusDay(day * -1); }; LocalDateTime.prototype.plusWeek = function (week) { return LocalDateTime.parseMoment(this.clone().momentDate.add(week, 'w')); }; LocalDateTime.prototype.minusWeek = function (week) { return this.plusWeek(week * -1); }; LocalDateTime.prototype.plusHour = function (hour) { return LocalDateTime.parseMoment(this.clone().momentDate.add(hour, 'h')); }; LocalDateTime.prototype.minusHour = function (hour) { return this.plusHour(hour * -1); }; LocalDateTime.prototype.plusMinute = function (minute) { return LocalDateTime.parseMoment(this.clone().momentDate.add(minute, 'm')); }; LocalDateTime.prototype.minusMinute = function (minute) { return this.plusMinute(minute * -1); }; LocalDateTime.prototype.plusSecond = function (second) { return LocalDateTime.parseMoment(this.clone().momentDate.add(second, 's')); }; LocalDateTime.prototype.minusSecond = function (second) { return this.plusSecond(second * -1); }; LocalDateTime.prototype.atStartOfDay = function () { return LocalDateTime.of(this.getYear(), this.getMonth(), this.getDate(), 0, 0, 0); }; LocalDateTime.prototype.atEndOfDay = function () { return LocalDateTime.of(this.getYear(), this.getMonth(), this.getDate(), 23, 59, 59); }; LocalDateTime.prototype.format = function (pattern) { if (pattern === void 0) { pattern = consts_1.DATE_TIME_FORMAT; } return this.momentDate.format(pattern); }; LocalDateTime.prototype.formatDay = function () { return this.momentDate.format(consts_1.DATE_FORMAT); }; LocalDateTime.prototype.toLocalDate = function () { return localDate_1.LocalDate.of(this.getYear(), this.getMonth(), this.getDate()); }; LocalDateTime.prototype.toLocalTime = function () { return localTime_1.LocalTime.of(this.getHour(), this.getMinute(), this.getSecond()); }; LocalDateTime.prototype.atStartOfMonth = function () { return LocalDateTime.of(this.getYear(), this.getMonth(), 1, 0, 0, 0); }; LocalDateTime.prototype.atEndOfMonth = function () { return LocalDateTime.of(this.getYear(), this.getMonth(), this.clone().atStartOfMonth().plusMonth(1).minusDay(1).getDate()); }; LocalDateTime.prototype.atStartOfWeek = function () { // moment.js 中文环境(zh-cn)下 firstDay是上周日而不是周一 var startOfWeek = this.momentDate.weekday(0); var year = startOfWeek.year(); var month = startOfWeek.month() + 1; var date = startOfWeek.date(); return LocalDateTime.of(year, month, date); }; LocalDateTime.prototype.atEndOfWeek = function () { // moment.js 需指定星期一为一周的第一天 var startOfWeek = this.momentDate.weekday(6); var year = startOfWeek.year(); var month = startOfWeek.month() + 1; var date = startOfWeek.date(); return LocalDateTime.of(year, month, date); }; LocalDateTime.prototype.setDate = function (day) { this.momentDate.date(day); return this; }; LocalDateTime.prototype.setHour = function (hour) { this.momentDate.hour(hour); return this; }; LocalDateTime.prototype.setMinute = function (minute) { this.momentDate.minute(minute); return this; }; LocalDateTime.prototype.setMonth = function (month) { this.momentDate.month(month - 1); return this; }; LocalDateTime.prototype.setSecond = function (second) { this.momentDate.second(second); return this; }; LocalDateTime.prototype.setYear = function (year) { this.momentDate.year(year); return this; }; LocalDateTime.prototype.toString = function () { return this.format(); }; return LocalDateTime; }(abstractDate_1.AbstractDate)); exports.LocalDateTime = LocalDateTime;