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);