UNPKG

java-localdatetime

Version:

### 日期工具库

164 lines (163 loc) 6.37 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.LocalDate = void 0; var moment = require("moment"); var abstractDate_1 = require("./abstractDate"); var localDateTime_1 = require("./localDateTime"); var consts_1 = require("./consts"); var LocalDate = /** @class */ (function (_super) { __extends(LocalDate, _super); function LocalDate(date) { var _this = _super.call(this) || this; _this.momentDate = date ? moment.utc(date, consts_1.DATE_FORMAT) : LocalDate.now().momentDate; return _this; } LocalDate.of = function (year, month, date) { return new LocalDate(year + '-' + month + '-' + date); }; LocalDate.now = function () { var _a = _super.buildDateProperty.call(this), year = _a.year, month = _a.month, date = _a.date; return LocalDate.of(year, month, date); }; LocalDate.parse = function (date) { return new LocalDate(date); }; LocalDate.parseMoment = function (moment) { var _a = _super.buildDateProperty.call(this, moment), year = _a.year, month = _a.month, date = _a.date; return LocalDate.of(year, month, date); }; LocalDate.getMonth = function () { return LocalDate.now().getMonth(); }; LocalDate.getListDay = function (star, end) { var localDateList = []; var starLocalDate = new LocalDate(star); var endLocalDate = new LocalDate(end); endLocalDate = endLocalDate.plusDay(1); while (starLocalDate.isBefore(endLocalDate)) { localDateList.push(starLocalDate.format()); starLocalDate = starLocalDate.plusDay(1); } return localDateList; }; LocalDate.prototype.clone = function () { return LocalDate.parseMoment(this.momentDate.clone()); }; LocalDate.prototype.getYear = function () { return this.year(); }; LocalDate.prototype.getMonth = function () { return this.month(); }; LocalDate.prototype.getDate = function () { return this.date(); }; LocalDate.prototype.getWeekDay = function () { return this.weekDay(); }; LocalDate.prototype.getWeek = function () { return this.week(); }; LocalDate.prototype.getTime = function () { return this.momentDate.valueOf(); }; LocalDate.prototype.weekday = function (num) { this.momentDate.weekday(num); return this; }; LocalDate.prototype.getListDay = function (start, end) { return this.getListDay(start, end); }; LocalDate.prototype.format = function (pattern) { if (pattern === void 0) { pattern = consts_1.DATE_FORMAT; } return this.momentDate.format(pattern); }; LocalDate.prototype.isSameDay = function (date) { return this.momentDate.isSame(date.momentDate); }; LocalDate.prototype.isBefore = function (date) { return this.momentDate.isBefore(date.momentDate); }; LocalDate.prototype.isAfter = function (date) { return this.momentDate.isAfter(date.momentDate); }; LocalDate.prototype.plusYear = function (year) { return LocalDate.parseMoment(this.clone().momentDate.add(year, 'y')); }; LocalDate.prototype.minusYear = function (year) { return this.plusYear(year * -1); }; LocalDate.prototype.plusMonth = function (month) { return LocalDate.parseMoment(this.clone().momentDate.add(month, 'M')); }; LocalDate.prototype.minusMonth = function (month) { return this.plusMonth(month * -1); }; LocalDate.prototype.plusDay = function (day) { return LocalDate.parseMoment(this.clone().momentDate.add(day, 'd')); }; LocalDate.prototype.minusDay = function (day) { return this.plusDay(day * -1); }; LocalDate.prototype.atStartOfDay = function () { return localDateTime_1.LocalDateTime.of(this.getYear(), this.getMonth(), this.getDate(), 0, 0, 0); }; LocalDate.prototype.toLocalDateTime = function () { return this.atStartOfDay(); }; LocalDate.prototype.atEndOfDay = function () { return localDateTime_1.LocalDateTime.of(this.getYear(), this.getMonth(), this.getDate(), 23, 59, 59); }; LocalDate.prototype.atStartOfMonth = function () { return LocalDate.of(this.getYear(), this.getMonth(), 1); }; LocalDate.prototype.atEndOfMonth = function () { return LocalDate.of(this.getYear(), this.getMonth(), this.clone().atStartOfMonth().plusMonth(1).minusDay(1).getDate()); }; LocalDate.prototype.atStartOfWeek = function () { // moment.js 需指定星期一为一周的第一天 var startOfWeek = this.momentDate.weekday(0); var year = startOfWeek.year(); var month = startOfWeek.month() + 1; var date = startOfWeek.date(); return LocalDate.of(year, month, date); }; LocalDate.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 LocalDate.of(year, month, date); }; LocalDate.prototype.setDate = function (day) { this.momentDate.date(day); return this; }; LocalDate.prototype.setMonth = function (month) { this.momentDate.month(month - 1); return this; }; LocalDate.prototype.setYear = function (year) { this.momentDate.year(year); return this; }; LocalDate.prototype.toString = function () { return this.format(); }; return LocalDate; }(abstractDate_1.AbstractDate)); exports.LocalDate = LocalDate;