Skip to content

Commit

Permalink
feat(format): format to string
Browse files Browse the repository at this point in the history
format dayjs object to a string
  • Loading branch information
iamkun committed Apr 12, 2018
1 parent 4665d0a commit 09719f4
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Dayjs {
this.utc = false
const args = this.parseConfig(config)
this.date = new Date(args)
this.timeZone = this.date.getTimezoneOffset() / 60
this.timeZoneString = String(this.timeZone * -1).replace(/^(.)?(\d)/, '$10$200').padStart(5, '+')
}

parseConfig(config) {
Expand All @@ -27,7 +29,8 @@ class Dayjs {
}

unix() {
const zonePad = this.utc ? this.date.getTimezoneOffset() * 60 * 1000 : 0
// timezone(hour) * 60 * 60 * 1000 => ms
const zonePad = !this.utc ? 0 : this.timeZone * 60 * 60 * 1000
return Math.floor((this.date.getTime() + zonePad) / 1000)
}

Expand All @@ -45,6 +48,57 @@ class Dayjs {
return this
}
}

format(formatStr = 'YYYY-MM-DDTHH:mm:ssZ') {
const weeks = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
const { date } = this
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const week = date.getDay()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()

return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g, (match) => {
switch (match) {
case 'YY':
return String(year).slice(-2)
case 'YYYY':
return String(year)
case 'M':
return String(month)
case 'MM':
return String(month).padStart(2, '0')
case 'D':
return String(day)
case 'DD':
return String(day).padStart(2, '0')
case 'd':
return String(week)
case 'dddd':
return weeks[week]
case 'H':
return String(hour)
case 'HH':
return String(hour).padStart(2, '0')
case 'm':
return String(minute)
case 'mm':
return String(minute).padStart(2, '0')
case 's':
return String(second)
case 'ss':
return String(second).padStart(2, '0')
case 'Z':
return this.timeZoneString.replace('00', ':00')
case 'ZZ':
return this.timeZoneString
default:
return match
}
})
}
}

export default config => (
Expand Down
52 changes: 52 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import moment from 'moment'
import dayjs from '../src'

test('Format no formatStr', () => {
expect(dayjs().format()).toBe(moment().format())
})

test('Format Year YY YYYY', () => {
expect(dayjs().format('YY')).toBe(moment().format('YY'))
expect(dayjs().format('YYYY')).toBe(moment().format('YYYY'))
})

test('Format Month M MM', () => {
expect(dayjs().format('M')).toBe(moment().format('M'))
expect(dayjs().format('MM')).toBe(moment().format('MM'))
})

test('Format Day of Month D DD 1 - 31', () => {
expect(dayjs().format('D')).toBe(moment().format('D'))
expect(dayjs().format('DD')).toBe(moment().format('DD'))
})

test('Format Day of Week d Sun - Sat', () => {
expect(dayjs().format('d')).toBe(moment().format('d'))
expect(dayjs().format('dddd')).toBe(moment().format('dddd'))
})

test('Format Hour H HH 24-hour', () => {
expect(dayjs().format('H')).toBe(moment().format('H'))
expect(dayjs().format('HH')).toBe(moment().format('HH'))
})

test('Format Minute m mm', () => {
expect(dayjs().format('m')).toBe(moment().format('m'))
expect(dayjs().format('mm')).toBe(moment().format('mm'))
})

test('Format Second s sss', () => {
expect(dayjs().format('s')).toBe(moment().format('s'))
expect(dayjs().format('ss')).toBe(moment().format('ss'))
})

test('Format Time Zone ZZ', () => {
expect(dayjs().format('Z')).toBe(moment().format('Z'))
expect(dayjs().format('ZZ')).toBe(moment().format('ZZ'))
})

test('Format Complex with other string - : / ', () => {
const string = 'YY-M-D / HH:mm:ss'
expect(dayjs().format(string)).toBe(moment().format(string))
})

0 comments on commit 09719f4

Please sign in to comment.