jhzh
2026-01-04 d73b91becc044ee107b94668504f6286be00d622
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import dayjs from 'dayjs'
import isLeapYear from 'dayjs/plugin/isLeapYear' // import plugin
import relativeTime from 'dayjs/plugin/relativeTime'
import durationPlug from 'dayjs/plugin/duration'
import 'dayjs/locale/zh-cn' // import locale
 
dayjs.extend(isLeapYear) // use plugin
dayjs.extend(relativeTime)
dayjs.extend(durationPlug)
dayjs.locale('zh-cn') // use locale
 
export const dateFormat = (date, template = 'YYYY-MM-DD') => {
  if (!date) return ''
  return dayjs(date).format(template)
}
 
export const dateDuration = (time, unit = 'milliseconds') => {
  const ctx = dayjs.duration(time, unit)
  const { $d: ret } = ctx
  return {
    ctx,
    ...ret,
  }
}
 
export const dateFromNow = (date, withoutSuffix = false) => {
  return dayjs(date).fromNow(withoutSuffix)
}
 
export default dayjs