gpt4 book ai didi

javascript - 从javascript中的字符串中获取差异(小时)

转载 作者:行者123 更新时间:2023-11-30 00:11:55 29 4
gpt4 key购买 nike

var d1 = new Date();
var d2 = new Date();
d2.setHours(d1.getHours() +01);
alert(d2);

这部分没问题。现在我试图在 (d1.getHours() +01); $string 中放置一个字符串,而不是 +01。像这样:

<select id="timezone-select">
<option value='-02:00'>GMT -02:00</option>
<option value='-01:00'>GMT -01:00</option>
<option value='+00:00'>GMT +00:00</option>
<option selected='' value='+01:00'>GMT +01:00</option>
<option value='+02:00'>GMT +02:00</option>
</select>

var str = document.getElementById('timezone-select').value;
var res = str.substr(0, 3);
var d1 = new Date();
var d2 = new Date();
d2.setHours(d1.getHours() res);
alert(d2);

有什么帮助吗?

最佳答案

尝试将 d2.setHours(d1.getHours() res) 替换为 d2.setHours(d1.getHours()+parseInt(res))

关于javascript - 从javascript中的字符串中获取差异(小时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36145668/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com