Skip to content

Commit

Permalink
feat: 🎸 transfer dateformat
Browse files Browse the repository at this point in the history
transfer添加dateformat方法
  • Loading branch information
liuxingyun2010 committed Feb 27, 2019
1 parent 66e87c9 commit 8de42b0
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
const dateFormat: (date: Date, fmt: string) => string = (date, fmt) => {
const o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'h+': date.getHours(), // 小时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
S: date.getMilliseconds() // 毫秒
}
let format = fmt
if (/(y+)/.test(fmt)) {
format = fmt.replace(
RegExp.$1,
String(date.getFullYear()).substr(4 - RegExp.$1.length)
)
}
if (/(wk)/.test(fmt)) {
const wks = '日一二三四五六'.split('')
format = fmt.replace(RegExp.$1, wks[date.getDay()])
}
for (const k in o) {
if (new RegExp(`(${k})`).test(format)) {
format = format.replace(
RegExp.$1,
RegExp.$1.length === 1
? (o as any)[k]
: `00${(o as any)[k]}`.substr(String((o as any)[k]).length)
)
}
}
return format
}

// 时间差,单位为小时
const offsetHours = (date1: Date, date2: Date): number => {
const NUM = 3600000
Expand Down Expand Up @@ -33,4 +67,4 @@ const stringToDate = (val: string): Date => {
return new Date()
}

export { offsetHours, offsetDays, stringToDate }
export { offsetHours, offsetDays, stringToDate, dateFormat }

0 comments on commit 8de42b0

Please sign in to comment.