JS將時間秒轉(zhuǎn)換成天小時分鐘秒的字符串
項目中需求是這樣,接口返回的數(shù)據(jù)中時間單位為秒,但前端顯示的時候需要更人性化的帶有單位(天,小時,分鐘,秒)的字符串;
轉(zhuǎn)換函數(shù)如下:
/** * 格式化秒 * @param int value 總秒數(shù) * @return string result 格式化后的字符串 */ function formatSeconds(value) { var theTime = parseInt(value);// 需要轉(zhuǎn)換的時間秒 var theTime1 = 0;// 分 var theTime2 = 0;// 小時 var theTime3 = 0;// 天 if(theTime > 60) { theTime1 = parseInt(theTime/60); theTime = parseInt(theTime%60); if(theTime1 > 60) { theTime2 = parseInt(theTime1/60); theTime1 = parseInt(theTime1%60); if(theTime2 > 24){ //大于24小時 theTime3 = parseInt(theTime2/24); theTime2 = parseInt(theTime2%24); } } } var result = ''; if(theTime > 0){ result = ""+parseInt(theTime)+"秒"; } if(theTime1 > 0) { result = ""+parseInt(theTime1)+"分"+result; } if(theTime2 > 0) { result = ""+parseInt(theTime2)+"小時"+result; } if(theTime3 > 0) { result = ""+parseInt(theTime3)+"天"+result; } return result; }
ps:下面看下js時間戳與時間日期間相互轉(zhuǎn)換
今天在工作中要將獲取到的時間轉(zhuǎn)換為時間戳,一時間竟不知道怎么用,于是不得不去查詢資料,這里特地做個筆記。
1、將日期轉(zhuǎn)換為時間戳。
要將日期轉(zhuǎn)換為時間戳,首先得先獲取到日期,這里可以直接指定日期,或者是使用當前日期。要獲取當前日期,我們可以使用new Date()來獲取。直接上代碼。
// (1)、將當前日期轉(zhuǎn)換為時間戳。 var now = new Date(); console.log(now.getTime()) // 將當前日期轉(zhuǎn)換為時間戳,getTime()方法可返回距1970年1月1日之間的毫秒數(shù)。也可以使用 +now ,該效果等同于now.getTime() // (2)、將指定日期轉(zhuǎn)換為時間戳。 var t = "2017-12-08 20:5:30"; // 月、日、時、分、秒如果不滿兩位數(shù)可不帶0. var T = new Date(t); // 將指定日期轉(zhuǎn)換為標準日期格式。Fri Dec 08 2017 20:05:30 GMT+0800 (中國標準時間) console.log(T.getTime()) // 將轉(zhuǎn)換后的標準日期轉(zhuǎn)換為時間戳。
2、將時間戳轉(zhuǎn)換為日期。
var t = 787986456465; // 當參數(shù)為數(shù)字的時候,那么這個參數(shù)就是時間戳,被視為毫秒,創(chuàng)建一個距離1970年1月一日指定毫秒的時間日期對象。 console.log(new Date(t)) // Wed Dec 21 1994 13:07:36 GMT+0800 (中國標準時間) var t2 = "2017-5-8 12:50:30"; console.log(new Date(t2)) // Mon May 08 2017 12:50:30 GMT+0800 (中國標準時間) var t3 = "2017-10-1"; console.log(new Date(t3)) // Sun Oct 01 2017 00:00:00 GMT+0800 (中國標準時間) 不設定時分秒,則默認轉(zhuǎn)換為00:00:00
將時間戳轉(zhuǎn)換為指定格式日期的方法封裝:
// 格式化日期,如月、日、時、分、秒保證為2位數(shù) function formatNumber (n) { n = n.toString() return n[1] ? n : '0' + n; } // 參數(shù)number為毫秒時間戳,format為需要轉(zhuǎn)換成的日期格式 function formatTime (number, format) { let time = new Date(number) let newArr = [] let formatArr = ['Y', 'M', 'D', 'h', 'm', 's'] newArr.push(time.getFullYear()) newArr.push(formatNumber(time.getMonth() + 1)) newArr.push(formatNumber(time.getDate())) newArr.push(formatNumber(time.getHours())) newArr.push(formatNumber(time.getMinutes())) newArr.push(formatNumber(time.getSeconds())) for (let i in newArr) { format = format.replace(formatArr[i], newArr[i]) } return format; }
如需要調(diào)用上述方法,使用formatTime(1545903266795, 'Y年M月D日 h:m:s')
或者formatTime(1545903266795, 'Y-M-D h:m:s')即可
總結(jié)
以上所述是小編給大家介紹的JS將時間秒轉(zhuǎn)換成天小時分鐘秒的字符串,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- JavaScript中各種時間轉(zhuǎn)換問題詳解(YYYY-MM-DD、時間戳、中國標準時間)
- 關(guān)于導入excel時js轉(zhuǎn)換時間的正確方式
- JS的時間格式化和時間戳轉(zhuǎn)換函數(shù)示例詳解
- js時間轉(zhuǎn)換毫秒的實例代碼
- 淺談在Vue.js中如何實現(xiàn)時間轉(zhuǎn)換指令
- Vue.js 時間轉(zhuǎn)換代碼及時間戳轉(zhuǎn)時間字符串
- js實現(xiàn)把時間戳轉(zhuǎn)換為yyyy-MM-dd hh:mm 格式(es6語法)
- JS中國標準時間轉(zhuǎn)化為年月日時分秒'yyyy-MM-dd hh:mm:ss'的示例詳解
相關(guān)文章
javascript中的有名函數(shù)和無名函數(shù)
javascript中的有名函數(shù)和無名函數(shù)...2007-10-10JavaScript中字符串(string)轉(zhuǎn)json的2種方法
這篇文章主要介紹了JavaScript中字符串(string)轉(zhuǎn)json的2種方法,兩種方法分別是使用js函數(shù)eval()和、使用jquery.parseJSON()方法,需要的朋友可以參考下2015-06-06javascript中等于(==)與全等(===)的區(qū)別說明
等于(==)的情況下 只要值相同就返回True。而全等(===)的時候需要值和類型都要匹配才能返回True.2011-01-01BootStrap 圖片樣式、輔助類樣式和CSS組件的實例詳解
這篇文章主要介紹了BootStrap 圖片樣式、輔助類樣式和CSS組件的實現(xiàn)代碼,圖文并茂介紹的非常詳細,需要的朋友參考下吧2017-01-01