1. 程式人生 > >js計算本地時間

js計算本地時間

 

獲取時間戳:

方法一

var  dateTime = new Date();//獲取本地時間
var nowTime = dateTime.getTime();//獲取本地毫秒,即當前時間
var  endtime = 1541689299195;//假設開始毫秒
var  cutTime = nowTime - endtime;//
var  cutTime2 = Math.floor((nowTime - endtime)/1000);//時間差轉化為秒數
var  cutTime2 = Math.floor((nowTime - endtime)/(1000*60));//時間差轉化為分鐘
var  cutTime2 = Math.floor((nowTime - endtime)/(1000*60*60);//獲取當前的小時
var  cutTime2 = Math.floor((nowTime - endtime)/(1000*60*60*24));//獲取天數
console.log("本地時間:::"+dateTime.getTime());
console.log("獲取時間:::"+endtime);
console.log("時間差ms:::"+cutTime);
console.log("時間差s:::"+cutTime2);

  方法二:


//時間戳
function timediff($begin_time, $end_time) {
var now = new Date().getTime()
if ($begin_time < $end_time) {
$starttime = $begin_time;
$endtime = $end_time;
} else {
$starttime = $end_time;
$endtime = $begin_time;
}
//計算天數 1s=1000ms
var $timediff = parseInt(($endtime - $starttime) / 1000);
var $days = parseInt($timediff / 86400);
//計算小時數
var $remain = ($timediff % 86400);
var $hours = parseInt($remain / 3600);
//計算分鐘數
var $remain = $remain % 3600;
var $mins = parseInt($remain / 60);
//計算秒數
var $secs = $remain % 60;
var $res = $days + "天" + $hours + "時" + $mins + "分" + $secs + "秒";
return $res;
}


//時間戳賦值
var timefunction = timeif(item.beginTime, item.endTime);
$itemDom.find('.timestamp').text(timefunction);