項(xiàng)目中需求是這樣,接口返回的數(shù)據(jù)中時(shí)間單位為秒,但前端顯示的時(shí)候需要更人性化的帶有單位(天,小時(shí),分鐘,秒)的字符串;
轉(zhuǎn)換函數(shù)如下:
/** * 格式化秒 * @param int value 總秒數(shù) * @return string result 格式化后的字符串 */function formatSeconds(value) { var theTime = parseInt(value);// 需要轉(zhuǎn)換的時(shí)間秒 var theTime1 = 0;// 分 var theTime2 = 0;// 小時(shí) 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小時(shí) 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)+"小時(shí)"+result; } if(theTime3 > 0) { result = ""+parseInt(theTime3)+"天"+result; } return result; }
ps:下面看下js時(shí)間戳與時(shí)間日期間相互轉(zhuǎn)換
今天在工作中要將獲取到的時(shí)間轉(zhuǎn)換為時(shí)間戳,一時(shí)間竟不知道怎么用,于是不得不去查詢資料,這里特地做個(gè)筆記。
1、將日期轉(zhuǎn)換為時(shí)間戳。
要將日期轉(zhuǎn)換為時(shí)間戳,首先得先獲取到日期,這里可以直接指定日期,或者是使用當(dāng)前日期。要獲取當(dāng)前日期,我們可以使用new Date()來獲取。直接上代碼。
// (1)、將當(dāng)前日期轉(zhuǎn)換為時(shí)間戳。 var now = new Date(); console.log(now.getTime()) // 將當(dāng)前日期轉(zhuǎn)換為時(shí)間戳,getTime()方法可返回距1970年1月1日之間的毫秒數(shù)。也可以使用 +now ,該效果等同于now.getTime()// (2)、將指定日期轉(zhuǎn)換為時(shí)間戳。 var t = "2017-12-08 20:5:30"; // 月、日、時(shí)、分、秒如果不滿兩位數(shù)可不帶0. var T = new Date(t); // 將指定日期轉(zhuǎn)換為標(biāo)準(zhǔn)日期格式。Fri Dec 08 2017 20:05:30 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間) console.log(T.getTime()) // 將轉(zhuǎn)換后的標(biāo)準(zhǔn)日期轉(zhuǎn)換為時(shí)間戳。
2、將時(shí)間戳轉(zhuǎn)換為日期。
var t = 787986456465; // 當(dāng)參數(shù)為數(shù)字的時(shí)候,那么這個(gè)參數(shù)就是時(shí)間戳,被視為毫秒,創(chuàng)建一個(gè)距離1970年1月一日指定毫秒的時(shí)間日期對(duì)象。console.log(new Date(t)) // Wed Dec 21 1994 13:07:36 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)var t2 = "2017-5-8 12:50:30";console.log(new Date(t2)) // Mon May 08 2017 12:50:30 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)var t3 = "2017-10-1";console.log(new Date(t3)) // Sun Oct 01 2017 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間) 不設(shè)定時(shí)分秒,則默認(rèn)轉(zhuǎn)換為00:00:00
將時(shí)間戳轉(zhuǎn)換為指定格式日期的方法封裝:
// 格式化日期,如月、日、時(shí)、分、秒保證為2位數(shù)function formatNumber (n) { n = n.toString() return n[1] ? n : '0' + n;}// 參數(shù)number為毫秒時(shí)間戳,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將時(shí)間秒轉(zhuǎn)換成天小時(shí)分鐘秒的字符串,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
新聞熱點(diǎn)
疑難解答