时间戳与时间差

ajax异步接口获取到时间戳
当前时间与该时间戳比较
求已经过去了多长时间??

function time_(now){
    // formatDate方法,将时间戳化为时间 2018-1-30 20:30:00;
    function formatDate(now) {
      console.log('now',now);
      var d = new Date(parseInt(now, 10) * 1000);
      var year = d.getFullYear();
      var month = d.getMonth() + 1;
      var date = d.getDate();
      var hour = d.getHours();
      var minute = d.getMinutes();
      return year + '-' + month + '-' + date + ' ' + hour + ':' + minute;
    };
    var startTime = formatDate(now);
    var s1 = new Date(startTime.replace(/-/g, "/")),
    s2 = new Date(),
    runTime = parseInt((s2.getTime() - s1.getTime()) / 1000);
    var year = Math.floor(runTime / 86400 / 365);
    runTime = runTime % (86400 * 365);
    var month = Math.floor(runTime / 86400 / 30);
    runTime = runTime % (86400 * 30);
    var day = Math.floor(runTime / 86400);
    runTime = runTime % 86400;
    var hour = Math.floor(runTime / 3600);
    runTime = runTime % 3600;
    var minute = Math.floor(runTime / 60);
    runTime = runTime % 60;
    var second = runTime;
    if (year < 0) {
    // 判断是否超过上面的时间
      return;
    } else {
      return '已过去了' + hour+'时'+minute+'分'+second+'秒'
    }
}

console.log(time_(res.data.start_time));

你可能感兴趣的:(时间戳与时间差)