javascript - 如何把時間戳轉換成日期類型的格式
問題描述
如何把時間戳轉換成日期類型的格式
問題解答
回答1:moment
http://momentjs.cn
看看要哪種格式
回答2:拿走不謝。我是新一代的活雷鋒
/** * 時間格式方法 * * @param {any} timeStamp 時間戳,秒級/毫秒級 * @param {any} type 格式化時間類型,默認 Y-M-D H:I:S * @returns {string} formatTime 格式化后的時間 例如: 2017-05-05 12:09:22 */function formatDate(timeStamp, type = ’Y-M-D H:I:S’, auto = false) { let time = (timeStamp + ’’).length === 10 ? new Date(parseInt(timeStamp) * 1000) : new Date(parseInt(timeStamp)); let _year = time.getFullYear(); let _month = (time.getMonth() + 1) < 10 ? ’0’ + (time.getMonth() + 1) : (time.getMonth() + 1); let _date = time.getDate() < 10 ? ’0’ + time.getDate() : time.getDate(); let _hours = time.getHours() < 10 ? ’0’ + time.getHours() : time.getHours(); let _minutes = time.getMinutes() < 10 ? ’0’ + time.getMinutes() : time.getMinutes(); let _secconds = time.getSeconds() < 10 ? ’0’ + time.getSeconds() : time.getSeconds(); let formatTime = ’’; let distinctTime = new Date().getTime() - time.getTime(); if (auto) {if (distinctTime <= (1 * 60 * 1000)) { // console.log(’一分鐘以內,以秒數(shù)計算’); let _s = Math.floor((distinctTime / 1000) % 60); formatTime = _s + ’秒前’;} else if (distinctTime <= (1 * 3600 * 1000)) { // console.log(’一小時以內,以分鐘計算’); let _m = Math.floor((distinctTime / (60 * 1000)) % 60); formatTime = _m + ’分鐘前’;} else if (distinctTime <= (24 * 3600 * 1000)) { // console.log(’一天以內,以小時計算’); let _h = Math.floor((distinctTime / (60 * 60 * 1000)) % 24); formatTime = _h + ’小時前’;} else if (distinctTime <= (30 * 24 * 3600 * 1000)) { let _d = Math.floor((distinctTime / (24 * 60 * 60 * 1000)) % 30); formatTime = _d + ’天前’; // console.log(’30天以內,以天數(shù)計算’);} else { // 30天以外只顯示年月日 formatTime = _year + ’-’ + _month + ’-’ + _date;} } else {switch (type) { case ’Y-M-D H:I:S’:formatTime = _year + ’-’ + _month + ’-’ + _date + ’ ’ + _hours + ’:’ + _minutes + ’:’ + _secconds;break; case ’Y-M-D H:I:S zh’:formatTime = _year + ’年’ + _month + ’月’ + _date + ’日 ’ + _hours + ’:’ + _minutes + ’:’ + _secconds;break; case ’Y-M-D H:I’:formatTime = _year + ’-’ + _month + ’-’ + _date + ’ ’ + _hours + ’:’ + _minutes;break; case ’Y-M-D H’:formatTime = _year + ’-’ + _month + ’-’ + _date + ’ ’ + _hours;break; case ’Y-M-D’:formatTime = _year + ’-’ + _month + ’-’ + _date;break; case ’Y-M-D zh’:formatTime = _year + ’年’ + _month + ’月’ + _date + ’日’;break; case ’Y-M’:formatTime = _year + ’-’ + _month;break; case ’Y’:formatTime = _year;break; case ’M’:formatTime = _month;break; case ’D’:formatTime = _date;break; case ’H’:formatTime = _hours;break; case ’I’:formatTime = _minutes;break; case ’S’:formatTime = _secconds;break; default:formatTime = _year + ’-’ + _month + ’-’ + _date + ’ ’ + _hours + ’:’ + _minutes + ’:’ + _secconds;break;} } // 返回格式化的日期字符串 return formatTime;}
相關文章:
1. mysql - sql 優(yōu)化問題,between比in好?2. mysql 5個left關鍵 然后再用搜索條件 幾千條數(shù)據(jù)就會卡,如何解決呢3. 個人主頁博客統(tǒng)計中的“進入博客”不能點擊4. java - 為什么hibernate查詢表集報錯?5. 關于nginx location匹配的問題6. java - spring 4.+ 利用reponse 下載文件看不到文件到底有多大,但是能下載成功 ?7. javascript - 請問 chrome 為什么會重復加載圖片資源?8. 就一臺服務器,mysql數(shù)據(jù)庫想實現(xiàn)自動備份,如何設計?9. java - Eclipse:為何方法默認未顯示注釋,鼠標懸浮卻可以看到注釋呢?10. pycharm運行python3.6突然出現(xiàn)R6034問題,請問如何處理?
