diff --git a/src/index.js b/src/index.js index 2ce109b2c..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) @@ -47,9 +51,13 @@ class Dayjs { } unix() { + return Math.floor(this.valueOf() / 1000) + } + + valueOf() { // timezone(hour) * 60 * 60 * 1000 => ms - const zonePad = !this.utc ? 0 : this.timeZone * 60 * 60 * 1000 - return Math.floor((this.$date.getTime() + zonePad) / 1000) + const zonePad = !this.$utc ? 0 : this.timeZone * 60 * 60 * 1000 + return this.$date.getTime() + zonePad } toString() { @@ -67,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': @@ -139,6 +168,19 @@ class Dayjs { } }) } + + diff(otherDate) { + 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 6fba2a643..8f714056c 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -50,3 +50,26 @@ test('Format Complex with other string - : / ', () => { expect(dayjs().format(string)).toBe(moment().format(string)) }) +it('Difference', () => { + const dateString = '20110101' + + const dayjsA = dayjs() + const dayjsB = dayjs(dateString) + + const momentA = moment() + const momentB = moment(dateString) + expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB)) +}) + +it('Unix Timestamp (milliseconds)', () => { + expect(dayjs().valueOf()).toBe(moment().valueOf()) +}) + +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', () => {