常用过滤器
1、计算某个日期距当前时间多少分钟/时/天
/** * @param {number} time */ export function timeAgo(time) { const between = Date.now() / 1000 - Number(time) if (between < 3600) { return pluralize(~~(between / 60), ' minute') } else if (between < 86400) { return pluralize(~~(between / 3600), ' hour') } else { return pluralize(~~(between / 86400), ' day') } }
2、首字母大写
/** * Upper case first char * @param {String} string */ export function uppercaseFirst(string) { return string.charAt(0).toUpperCase() + string.slice(1) }
3、秒时间戳转 yyyy-MM-dd HH:mm:ss
/** * * @param {*} originVal 传进来的时间戳 * @returns yyyy-MM-dd HH:mm:ss */ export function timeStampFormat(originVal) { const dt = new Date(originVal * 1000) const y = dt.getFullYear() const m = (dt.getMonth() + 1 + '').padStart(2, '0') const d = (dt.getDate() + '').padStart(2, '0') const hh = (dt.getHours() + '').padStart(2, '0') const mm = (dt.getMinutes() + '').padStart(2, '0') const ss = (dt.getSeconds() + '').padStart(2, '0') return `${y}-${m}-${d} ${hh}:${mm}:${ss}` }
4、国际时间2018-11-04T08:00:00.000+0000 转 yyyy-MM-dd HH:mm:ss
/**
*
* @param {*} date
* @returns
*/
export function dateTimeFormat(date) { const json_date = new Date(date).toJSON() console.log(json_date) return new Date(+new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '') }
5、国际时间 2018-11-04T08:00:00.000+0000 转日期 yyyy-MM-dd
/** * * @param {*} date * @returns */ export function dateTimeFormatData(date) { const d = new Date(date) return d.getFullYear() + '-' + (d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1) + '-' + (d.getDate() < 10 ? '0' + d.getDate() : d.getDate()) }