3 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
const now = new Date();
let d = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 18, 35, 0);
console.log(d.getUTCHours(), d.getUTCMinutes());

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊
你似乎把事情復(fù)雜化了。要獲取 UTC 時(shí)間,只需使用getUTC方法獲取小時(shí)和分鐘值。
要獲得 UTC 時(shí)間的等效本地時(shí)間,請使用setUTC方法設(shè)置小時(shí)和分鐘值,例如
// Helper to pad single digits
function z(n) {
return ('0' + n).slice(-2);
}
let d = new Date();
let localTime = z(d.getHours()) + ':' + z(d.getMinutes());
let utcTime = z(d.getUTCHours()) + ':' + z(d.getUTCMinutes());
// Print local time for d
console.log('Local time: ' + localTime);
// Print UTC time for d
console.log('UTC time: ' + utcTime);
// Set the time using UTC time in HH:mm format
utcTime = "13:05";
let [utcH, utcM] = utcTime.split(':');
d.setUTCHours(utcH, utcM);
console.log('At UTC ' + utcTime + ' the local time is ' + z(d.getHours()) + ':' + z(d.getMinutes()));

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
這里是toLocaleTimeString方法
const now = new Date();
let d = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 13, 5, 0);
console.log('locale', new Date(d).toLocaleTimeString('ru-kz', { timeStyle: 'short' }))
console.log('utc', new Date(d).getUTCHours() + ':' + new Date(d).getUTCMinutes())
添加回答
舉報(bào)