JavaScript(JS) 時間顯示(時區)

JavaScript(JS) 時間顯示(時區)

JavaScript(JS) 時間顯示(時區)


資料來源: https://www.cythilya.tw/2022/10/19/javascript-date-timezone/

線上執行(Online): https://www.programiz.com/javascript/online-compiler/


Code

function pad2(n) {
    return (n < 10 ? '0' : '') + n;
}

//var unixTimestamp = 1675406965;
//var date = new Date(unixTimestamp * 1000);
var date=new Date();
date.setUTCHours(date.getUTCHours()+8);
    var month = pad2(date.getMonth() + 1);//months (0-11)
    var day = pad2(date.getDate());//day (1-31)
    var year = date.getFullYear();
    var hour = pad2(date.getHours());
    var minute = pad2(date.getMinutes());
console.log(year+'/'+month+'/'+day+' '+hour+':'+minute);

//----------
//未切換時區
var date01=new Date();
    var month = pad2(date01.getMonth() + 1);//months (0-11)
    var day = pad2(date01.getDate());//day (1-31)
    var year = date01.getFullYear();
    var hour = pad2(date01.getHours());
    var minute = pad2(date01.getMinutes());
console.log(year+'/'+month+'/'+day+' '+hour+':'+minute);   

2 thoughts on “JavaScript(JS) 時間顯示(時區)

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *