1 function timesFun(timesData) { 2 //如果時間格式是正確的,那下面這一步轉化時間格式就可以不用了 3 var dateBegin = new Date(timesData); //將-轉化為/,使用new Date 4 var dateEnd = new Date(); //獲取當前時間 5 var dateDiff = dateEnd.getTime() - dateBegin.getTime(); //時間差的毫秒數 6 console.log(dateBegin,dateDiff, '111111111111') 7 console.log(dateEnd, dateDiff, '22222222222222') 8 var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //計算出相差天數 9 var leave1 = dateDiff % (24 * 3600 * 1000); //計算天數后剩余的毫秒數 10 var hours = Math.floor(leave1 / (3600 * 1000)); //計算出小時數 11 //計算相差分鍾數 12 var leave2 = leave1 % (3600 * 1000); //計算小時數后剩余的毫秒數 13 var minutes = Math.floor(leave2 / (60 * 1000)); //計算相差分鍾數 14 //計算相差秒數 15 var leave3 = leave2 % (60 * 1000); //計算分鍾數后剩余的毫秒數 16 var seconds = Math.round(leave3 / 1000); 17 var timesString = ""; 18 19 if (dayDiff != 0) { 20 timesString = dayDiff + "天之前"; 21 } else if (dayDiff == 0 && hours != 0) { 22 timesString = hours + "小時之前"; 23 } else if (dayDiff == 0 && hours == 0) { 24 timesString = minutes + "分鍾之前"; 25 } 26 27 return { 28 timesString: timesString 29 }; 30 } 31 var timesData = "2022-01-25 01:25:02"; 32 console.log(timesFun(timesData))
1 function valideNull(val) { 2 return typeof val == 'undefined' || val == '' || val == null || JSON.stringify(val) == '{}'; 3 } 4 function zeroize(num) { 5 return (String(num).length == 1 ? '0' : '') + num; 6 } 7 8 function timestampFormat(time) { 9 let timestamp = ''; 10 if (!valideNull(time)) { 11 var arr = time.replace(/ |:/g, '-').split('-'); 12 let date = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], arr[3] - 8, arr[4])); 13 timestamp = date.getTime() / 1000; 14 } else { 15 return ''; 16 } 17 18 19 var curTimestamp = parseInt(new Date().getTime() / 1000); //當前時間戳 20 var timestampDiff = curTimestamp - timestamp; // 參數時間戳與當前時間戳相差秒數 21 var curDate = new Date(curTimestamp * 1000); // 當前時間日期對象 22 var tmDate = new Date(timestamp * 1000); // 參數時間戳轉換成的日期對象 23 var Y = tmDate.getFullYear(), 24 m = tmDate.getMonth() + 1, 25 d = tmDate.getDate(); 26 var H = tmDate.getHours(), 27 i = tmDate.getMinutes(); 28 var nh = curDate.getHours(); 29 var ch = nh - H; 30 31 // s = tmDate.getSeconds() 32 // var nh = curDate.getHours(), 33 if (timestampDiff < 60) { 34 // 一分鍾以內 35 return '剛剛'; 36 } else if (timestampDiff < 3600) { 37 // 一小時前之內 38 return Math.floor(timestampDiff / 60) + '分鍾前'; 39 } else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) { 40 41 return ch + '小時前'; 42 } else { 43 var newDate = new Date((curTimestamp - 86400) * 1000); // 參數中的時間戳加一天轉換成的日期對象 44 var newDate2 = new Date((curTimestamp - (86400 * 2)) * 1000); //參數中的時間戳加兩天轉換成的日期對象 45 var newDate3 = new Date((curTimestamp - (86400 * 3)) * 1000); //參數中的時間戳加三天轉換成的日期對象 46 if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) { 47 return '一天前 '; 48 } else if (newDate2.getFullYear() == Y && newDate2.getMonth() + 1 == m && newDate2.getDate() == d) { 49 return '兩天前'; 50 } else if (newDate3.getFullYear() == Y && newDate3.getMonth() + 1 == m && newDate3.getDate() == d) { 51 return '三天前'; 52 } else { 53 return Y + '-' + zeroize(m) + '-' + zeroize(d); 54 } 55 } 56 } 57 58 var timesData = "2022-02-08 11:47:02"; 59 console.log(timestampFormat(timesData))