js 日期格式化代码
分享一个前端实用的 js 日期格式化代码,相当给力。
export function getFillDate(key) { if(key < 10) { return `0${key}`; }else{ return `${key}`; } } /** * 时间戳转化为年月日 * @param times 时间戳 * @param ymd 格式类型(yyyy-mm-dd,yyyy/mm/dd) * @param hms 可选,格式类型(hh,hh:mm,hh:mm:ss) * @returns {年月日} */ export function dateFomat (times, ymd, hms) { const oDate = new Date(times) const oYear = oDate.getFullYear() const oMonth = oDate.getMonth() + 1 const oDay = oDate.getDate() const oHour = oDate.getHours() const oMin = oDate.getMinutes() const oSec = oDate.getSeconds() let oTime // 最后拼接时间 // 年月日格式 switch (ymd) { case 'yyyy-mm-dd': oTime = oYear + '-' + getFillDate(oMonth) + '-' + getFillDate(oDay) break case 'yyyy/mm/dd': oTime = oYear + '/' + getFillDate(oMonth) + '/' + getFillDate(oDay) break } // 时分秒格式 switch (hms) { case 'hh': oTime = oTime + ' ' + getFillDate(oHour) break case 'hh:mm': oTime = oTime + ' ' + getFillDate(oHour) + ':' + getFillDate(oMin) break case 'hh:mm:ss': oTime = oTime + ' ' + getFillDate(oHour) + ':' + getFillDate(oMin) + ':' + getFillDate(oSec) break } return oTime }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » js 日期格式化代码
码云笔记 » js 日期格式化代码