diff --git a/src/index.js b/src/index.js index c695ebf59..36b8abeec 100644 --- a/src/index.js +++ b/src/index.js @@ -7,9 +7,13 @@ const padStart = (string, length, pad) => { class Dayjs { constructor(config) { - this.utc = false + this.$utc = false const args = this.parseConfig(config) this.$date = new Date(args) + this.init() + } + + init() { this.timeZone = this.$date.getTimezoneOffset() / 60 this.timeZoneString = padStart(String(this.timeZone * -1).replace(/^(.)?(\d)/, '$10$200'), 5, '+') this.$year = this.$date.getFullYear() @@ -25,7 +29,7 @@ class Dayjs { if (!config) return new Date() if (config instanceof Date) return config if (/^(\d){8}$/.test(config)) { - this.utc = true + this.$utc = true const y = config.substr(0, 4) const m = config.substr(4, 2) const d = config.substr(6, 2) @@ -52,7 +56,7 @@ class Dayjs { valueOf() { // timezone(hour) * 60 * 60 * 1000 => ms - const zonePad = !this.utc ? 0 : this.timeZone * 60 * 60 * 1000 + const zonePad = !this.$utc ? 0 : this.timeZone * 60 * 60 * 1000 return this.$date.getTime() + zonePad } @@ -71,7 +75,28 @@ class Dayjs { } } + set(string, int) { + switch (string) { + case 'date': + this.$date.setDate(int) + break + case 'month': + this.$date.setMonth(int) + break + default: + break + } + this.init() + } + add(number, string) { + if (['M', 'months'].indexOf(string) > -1) { + const date = this.clone() + date.set('date', 1) + date.set('month', this.month() + number) + date.set('date', Math.min(this.date(), date.daysInMonth())) + return date + } let step switch (string) { case 'm': @@ -148,6 +173,14 @@ class Dayjs { const other = otherDate instanceof Dayjs ? otherDate : new Dayjs(otherDate) return this.valueOf() - other.valueOf() } + + daysInMonth() { + return new Dayjs(new Date(this.year(), this.month() + 1, 0)).date() + } + + clone() { + return Object.assign(Object.create(this), this) + } } export default config => ( diff --git a/test/display.test.js b/test/display.test.js index 1802e1d30..8f714056c 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -61,11 +61,15 @@ it('Difference', () => { expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB)) }) -it('Unix Timestamp (milliseconds) ', () => { +it('Unix Timestamp (milliseconds)', () => { expect(dayjs().valueOf()).toBe(moment().valueOf()) }) -it('Unix Timestamp (seconds) ', () => { +it('Unix Timestamp (seconds)', () => { expect(dayjs().unix()).toBe(moment().unix()) }) +it('Days in Month', () => { + expect(dayjs().daysInMonth()).toBe(moment().daysInMonth()) +}) + diff --git a/test/manipulate.test.js b/test/manipulate.test.js index e75940fbc..efab1bac5 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -24,6 +24,8 @@ test('Add Time days', () => { expect(dayjs().add(1, 'weeks').unix()).toBe(moment().add(1, 'weeks').unix()) expect(dayjs().add(1, 'd').unix()).toBe(moment().add(1, 'd').unix()) expect(dayjs().add(1, 'days').unix()).toBe(moment().add(1, 'days').unix()) + expect(dayjs().add(1, 'M').unix()).toBe(moment().add(1, 'M').unix()) + expect(dayjs('20111031').add(1, 'months').unix()).toBe(moment('20111031').add(1, 'months').unix()) }) test('Subtract Time days', () => {