3 回答
TA贡献1831条经验 获得超10个赞
const now = new Date();
let d = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 18, 35, 0);
console.log(d.getUTCHours(), d.getUTCMinutes());
TA贡献1817条经验 获得超6个赞
你似乎把事情复杂化了。要获取 UTC 时间,只需使用getUTC方法获取小时和分钟值。
要获得 UTC 时间的等效本地时间,请使用setUTC方法设置小时和分钟值,例如
// 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贡献2016条经验 获得超9个赞
这里是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())
添加回答
举报