第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何從UTC字符串中獲取小時(shí)和分鐘?

如何從UTC字符串中獲取小時(shí)和分鐘?

慕尼黑8549860 2021-10-29 16:10:22
我想將日期時(shí)間從本地時(shí)間轉(zhuǎn)換為 UTC,然后將該 UTC 時(shí)間解碼回我的原始本地時(shí)間。要將本地時(shí)間轉(zhuǎn)換為 UTC,我使用了下面的代碼,效果很好。const now = new Date();    let d = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), 13, 5, 0);console.log(new Date(d).getHours(), new Date(d).getMinutes());以上代碼提供的UTC時(shí)間為+5:30,輸出為18,35(18:35:00)在這里,要將 18,35 (18:35:00) 轉(zhuǎn)換回 13:05:00,我使用了以下代碼const now = new Date();let d = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 18, 35, 0);console.log(d.toUTCString());上面的代碼為我提供了一個(gè)字符串格式的日期,它的時(shí)間是 13:05:00,這是正確的。現(xiàn)在,我想從這串日期中獲取小時(shí)和分鐘。我試過添加:new Date(d.UTCString()) 但是,它為我提供了 18 小時(shí)和 35 分鐘,而我想要 13 小時(shí)和 05 分鐘。請幫我解決一下這個(gè)。謝謝你。
查看完整描述

3 回答

?
慕哥6287543

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());


查看完整回答
反對 回復(fù) 2021-10-29
?
慕的地6264312

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()));



查看完整回答
反對 回復(fù) 2021-10-29
?
慕沐林林

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())


查看完整回答
反對 回復(fù) 2021-10-29
  • 3 回答
  • 0 關(guān)注
  • 275 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)