Skip to content

Commit

Permalink
feat: Add static method dayjs.utc to help migration from Moment.js
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Sep 27, 2018
1 parent beb80fc commit e640b57
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ declare namespace dayjs {
utcOffset(): number
}

export function utc(config?: ConfigType, option?: OptionType): Dayjs

export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void

export function extend(plugin: PluginFunc, option?: ConfigType): Dayjs
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@ class Dayjs {
}
}

dayjs.utc = (date, c) => {
const cfg = { ...c, utc: true }
return dayjs(date, cfg)
}

dayjs.extend = (plugin, option) => {
plugin(option, Dayjs, dayjs)
return dayjs
Expand Down
7 changes: 7 additions & 0 deletions test/utc/utc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ it('Returns an UTC instance', () => {
expect(instance.hour()).toEqual(19)
expect(instance.minute()).toEqual(34)
})

it('Static method creates an UTC instance', () => {
const instance = dayjs.utc('2018-09-06T19:34:28.657Z')
expect(instance.$u).toBeTruthy()
expect(instance.hour()).toEqual(19)
expect(instance.minute()).toEqual(34)
})

0 comments on commit e640b57

Please sign in to comment.