Skip to content

Commit

Permalink
fix(add): add month
Browse files Browse the repository at this point in the history
add month
  • Loading branch information
iamkun committed Apr 13, 2018
1 parent 8d127dc commit 25c9b96
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
39 changes: 36 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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':
Expand Down Expand Up @@ -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 => (
Expand Down
8 changes: 6 additions & 2 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

2 changes: 2 additions & 0 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 25c9b96

Please sign in to comment.