時間戳 2020-09-21轉Tue Sep 29 2020 08:00:00 GMT+0800 (中國標準時間)

2020-09-30 17:00:14

2020-09-21轉Tue Sep 29 2020 08:00:00 GMT+0800 (中國標準時間)

function formatterDate (date) {
    let result = new Date(date);
    return result;
}

時間差 Tue Sep 29 2020 08:00:00 GMT+0800 (中國標準時間)轉 天數

function getTime(starttime,endtime){
	var dateDiff = endtime.getTime() - starttime.getTime(); //時間差的毫秒數
	var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //計算出相差天數
	console.log(dayDiff + "天 ")
}

時間差 計算兩個時間相差的幾年幾月幾日幾時幾分幾秒

starttime和endtime格式為 Tue Sep 29 2020 08:00:00 GMT+0800 才能用下面的方法

function calculationTime(starttime,endtime) {
	var runTime;
	runTime = parseInt((endtime.getTime() - starttime.getTime()) / 1000);
	var years = Math.floor(runTime / 86400 / 365);
	runTime = runTime % (86400 * 365);
	var moths = Math.floor(runTime / 86400 / 30);
	runTime = runTime % (86400 * 30);
	var days = 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;
    console.log(years ,moths ,days,hour ,minute ,second  );
};