Skip to content

Commit

Permalink
Merge pull request #94 from xx45/dev
Browse files Browse the repository at this point in the history
add h hh a A format
  • Loading branch information
iamkun authored May 3, 2018
2 parents 73a89c3 + 37af709 commit faa0430
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 28 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
English | [简体中文](./ReadMe.zh-CN.md)
English | [简体中文](./README.zh-CN.md)

<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
Expand Down Expand Up @@ -332,21 +332,25 @@ List of all available formats:
| `YY` | 18 | Two digit year |
| `YYYY` | 2018 | Four digit year |
| `M` | 1-12 | The month, beginning at 1 |
| `MM` | 01-12 | The month, with preceeding 0 |
| `MM` | 01-12 | The month, 2-digits |
| `MMM` | Jan-Dec | The abbreviated month name |
| `MMMM` | January-December | The full month name |
| `D` | 1-31 | The day of the month |
| `DD` | 01-31 | The day of the month, preceeding 0 |
| `DD` | 01-31 | The day of the month, 2-digits |
| `d` | 0-6 | The day of the week, with Sunday as 0 |
| `dddd` | Sunday-Saturday | The name of the day of the week |
| `H` | 0-23 | The hour |
| `HH` | 00-23 | The hour, with preceeding 0 |
| `HH` | 00-23 | The hour, 2-digits |
| `h` | 1-12 | The hour, 12-hour clock |
| `hh` | 01-12 | The hour, 12-hour clock, 2-digits |
| `m` | 0-59 | The minute |
| `mm` | 00-59 | The minute, with preceeding 0 |
| `mm` | 00-59 | The minute, 2-digits |
| `s` | 0-59 | The second |
| `ss` | 00-59 | The second, with preceeding 0 |
| `ss` | 00-59 | The second, 2-digits |
| `Z` | +5:00 | The offset from UTC |
| `ZZ` | +0500 | The offset from UTC with preceeding 0 |
| `ZZ` | +0500 | The offset from UTC, 2-digits |
| `A` | AM PM | |
| `a` | am pm | |

#### Difference

Expand Down
2 changes: 1 addition & 1 deletion ReadMe.zh-CN.md → README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[English](./ReadMe.md) | 简体中文
[English](./README.md) | 简体中文
<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
alt="Day.js"></a></p>
Expand Down
9 changes: 9 additions & 0 deletions src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ export const M = 'month'
export const Q = 'quarter'
export const Y = 'year'
export const DATE = 'date'

export const WEEKS = 'Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday'.split('.')
export const MONTHS = 'January.February.March.April.May.June.July.August.September.October.November.December'.split('.')

export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'

// regex
export const REGEX_PARSE = /^(\d{4})-?(\d{2})-?(\d{1,2})$/
export const REGEX_FORMAT = /Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}/g
39 changes: 23 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const parseConfig = (config) => {
if (!config) return new Date()
if (config instanceof Date) return config
// eslint-disable-next-line no-cond-assign
if (reg = String(config).match(/^(\d{4})-?(\d{2})-?(\d{1,2})$/)) {
if (reg = String(config).match(C.REGEX_PARSE)) {
// 2018-08-08 or 20180808
return new Date(reg[1], reg[2] - 1, reg[3])
}
Expand Down Expand Up @@ -90,7 +90,8 @@ class Dayjs {
return this.$d.getTime()
}

startOf(units, isStartOf = true) { // isStartOf -> endOf
startOf(units, startOf) { // startOf -> endOf
const isStartOf = startOf !== undefined ? startOf : true
const unit = Utils.prettyUnit(units)
const instanceFactory = (d, m, y = this.$y) => {
const ins = new Dayjs(new Date(y, m, d))
Expand Down Expand Up @@ -207,11 +208,9 @@ class Dayjs {
return this.add(number * -1, string)
}

format(formatStr = 'YYYY-MM-DDTHH:mm:ssZ') {
const weeks = 'Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday'.split('.')
const months = 'January.February.March.April.May.June.July.August.September.October.November.December'.split('.')

return formatStr.replace(/Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g, (match) => {
format(formatStr) {
const str = formatStr || C.FORMAT_DEFAULT
return str.replace(C.REGEX_FORMAT, (match) => {
switch (match) {
case 'YY':
return String(this.$y).slice(-2)
Expand All @@ -220,31 +219,39 @@ class Dayjs {
case 'M':
return String(this.$M + 1)
case 'MM':
return Utils.padStart(String(this.$M + 1), 2, '0')
return Utils.padStart(this.$M + 1, 2, '0')
case 'MMM':
return months[this.$M].slice(0, 3)
return C.MONTHS[this.$M].slice(0, 3)
case 'MMMM':
return months[this.$M]
return C.MONTHS[this.$M]
case 'D':
return String(this.$D)
case 'DD':
return Utils.padStart(String(this.$D), 2, '0')
return Utils.padStart(this.$D, 2, '0')
case 'd':
return String(this.$W)
case 'dddd':
return weeks[this.$W]
return C.WEEKS[this.$W]
case 'H':
return String(this.$H)
case 'HH':
return Utils.padStart(String(this.$H), 2, '0')
return Utils.padStart(this.$H, 2, '0')
case 'h':
case 'hh':
if (this.$H === 0) return 12
return Utils.padStart(this.$H < 13 ? this.$H : this.$H - 12, match === 'hh' ? 2 : 1, '0')
case 'a':
return this.$H < 12 ? 'am' : 'pm'
case 'A':
return this.$H < 12 ? 'AM' : 'PM'
case 'm':
return String(this.$m)
case 'mm':
return Utils.padStart(String(this.$m), 2, '0')
return Utils.padStart(this.$m, 2, '0')
case 's':
return String(this.$s)
case 'ss':
return Utils.padStart(String(this.$s), 2, '0')
return Utils.padStart(this.$s, 2, '0')
case 'Z':
return `${this.$zoneStr.slice(0, -2)}:00`
default: // 'ZZ'
Expand All @@ -253,7 +260,7 @@ class Dayjs {
})
}

diff(input, units, float = false) {
diff(input, units, float) {
const unit = Utils.prettyUnit(units)
const that = input instanceof Dayjs ? input : new Dayjs(input)
const diff = this - that
Expand Down
9 changes: 5 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export const padStart = (string, length, pad) => {
if (!string || string.length >= length) return string
return `${Array((length + 1) - string.length).join(pad)}${string}`
const s = String(string)
if (!string || s.length >= length) return string
return `${Array((length + 1) - s.length).join(pad)}${string}`
}

export const isNumber = n => (!Number.isNaN(parseFloat(n)) && Number.isFinite(n))

export const monthDiff = (a, b) => {
// function from moment.js monthDiff
// function from moment.js in order to keep the same result
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
const anchor = a.clone().add(wholeMonthDiff, 'months')
let anchor2
Expand All @@ -18,7 +19,7 @@ export const monthDiff = (a, b) => {
anchor2 = a.clone().add(wholeMonthDiff + 1, 'months')
adjust = (b - anchor) / (anchor2 - anchor)
}
return Number(-(wholeMonthDiff + adjust)) || 0
return Number(-(wholeMonthDiff + adjust))
}

export const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))
Expand Down
24 changes: 24 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ it('Format Hour H HH 24-hour', () => {
expect(dayjs().format('HH')).toBe(moment().format('HH'))
})

it('Format Hour h hh 12-hour', () => {
MockDate.set(new Date('2018-05-02T00:00:00.000'))
expect(dayjs().format('h')).toBe(moment().format('h'))
expect(dayjs().format('hh')).toBe(moment().format('hh'))

MockDate.set(new Date('2018-05-02T01:00:00.000'))
expect(dayjs().format('h')).toBe(moment().format('h'))
expect(dayjs().format('hh')).toBe(moment().format('hh'))

MockDate.set(new Date('2018-05-02T23:00:00.000'))
expect(dayjs().format('h')).toBe(moment().format('h'))
expect(dayjs().format('hh')).toBe(moment().format('hh'))
})

it('Format meridiens a A am / pm', () => {
MockDate.set(new Date('2018-05-02T01:00:00.000'))
expect(dayjs().format('a')).toBe(moment().format('a'))
expect(dayjs().format('A')).toBe(moment().format('A'))

MockDate.set(new Date('2018-05-02T23:00:00.000'))
expect(dayjs().format('a')).toBe(moment().format('a'))
expect(dayjs().format('A')).toBe(moment().format('A'))
})

it('Format Minute m mm', () => {
expect(dayjs().format('m')).toBe(moment().format('m'))
expect(dayjs().format('mm')).toBe(moment().format('mm'))
Expand Down

0 comments on commit faa0430

Please sign in to comment.