happy-utils
Version:
共用函数库,提取出大部分业务需要的共用的函数。
54 lines (44 loc) • 1.4 kB
JavaScript
const {
isSameDay,
getCurrentDay,
dateFormat,
getTimestamp,
getCurrentWeek,
getBeforeDate,
formatWeek,
formatTimes,
} = require('../date')
const d1 = new Date('2017-08-12')
const d2 = new Date('2017-08-11')
const d3 = new Date(1328119322000)
// 判断是否相同日期
const isd = isSameDay(d1, d2)
console.log(isd)
// 获取日期,不传就是获取当前日期
const currentDay = getCurrentDay()
const currentDay2 = getCurrentDay(d2)
const currentDay3 = getCurrentDay(d3)
console.log(currentDay, currentDay2, currentDay3)
// 日期格式化
const df = dateFormat(1328119322000, 'yyyy-MM-dd')
console.log(df)
// 获取时间戳
const timestamp = getTimestamp('2017-08-11')
const timestamp2 = getTimestamp('2017-08-11 12:20:21')
console.log(timestamp, timestamp2)
// 获取星期几
const currentWeek = getCurrentWeek(d1, 'zh')
console.log(currentWeek)
// 获取前几天日期
const beforeDate = getBeforeDate(100)
console.log(beforeDate)
// 格式化周几到周几
const weeks1 = [1, 5, 6, 7]
const weeks2 = [1, 2, 3, 5, 6, 7]
console.log(formatWeek(weeks1))
console.log(formatWeek(weeks2))
// 格式化几点到几点
const times1 = [{ "startTime": "08:00", "endTime": "20:00" }]
const times2 = [{ "startTime": "08:00", "endTime": "20:00" }, { "startTime": "07:00", "endTime": "21:00" }]
console.log(formatTimes(times1))
console.log(formatTimes(times2))