欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS將時間秒轉(zhuǎn)換成天小時分鐘秒的字符串

 更新時間:2019年07月10日 09:28:49   作者:江峰★  
最近小編接到這樣的項目需求,接口返回的數(shù)據(jù)中時間單位為秒,但前端顯示的時候需要更人性化的帶有單位(天,小時,分鐘,秒)的字符串;下面小編給大家?guī)韺嵗a,感興趣的朋友跟隨小編一起看看吧

項目中需求是這樣,接口返回的數(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)載,煩請注明出處,謝謝!

相關(guān)文章

最新評論