Skip to content

Commit

Permalink
Merge pull request #17 from xx45/dev
Browse files Browse the repository at this point in the history
feat(diff valueOf): diff valueOf
  • Loading branch information
iamkun authored Apr 13, 2018
2 parents 74b0bc1 + 25c9b96 commit d63456a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
50 changes: 46 additions & 4 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 @@ -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() {
Expand All @@ -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':
Expand Down Expand Up @@ -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 => (
Expand Down
23 changes: 23 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

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 d63456a

Please sign in to comment.