gpt4 book ai didi

javascript - 使用选择选项转换时间。使用 JavaScript

转载 作者:行者123 更新时间:2023-11-28 06:03:58 24 4
gpt4 key购买 nike

I saw this code on jsfiddle http://jsfiddle.net/93pEd/174/. I decided to modify the code for my project because it's kinda similar. I added another drop down as origin (left) convert to (right)

你们能帮忙如何转换时间并将其放入输入文本字段吗这是我添加/编辑的代码附注我没有更改 JavaScript 代码。

$(function(){
$('.location').each(function() {
var timeZone = $(this).data('tz');

var now = moment().tz(timeZone).format('HH:mm');
$(this).append( now );
});
});
<select>
<option><span class="location" data-tz="Africa/Tripoli">Tripoli: </span></span><br /><option>
<option><span class="location" data-tz="Europe/London">London: </span><option>
<option><span class="location" data-tz="Europe/London">London: </span><option>
<option><span class="location" data-tz="Asia/Tokyo">Tokyo </span><option>
</select>
<p>CONVERT TO</p>
<select>
<option><span class="location" data-tz="Africa/Tripoli">Tripoli: </span></span><br /><option>
<option><span class="location" data-tz="Europe/London">London: </span><option>
<option><span class="location" data-tz="Europe/London">London: </span><option>
<option><span class="location" data-tz="Asia/Tokyo">Tokyo </span><option>

</select>

<input type="text" id="gettime" value="">

最佳答案

$(document).ready(function(){

// get Bombay time
alert(calcTime('Bombay', '+5.5'));

// get Singapore time
alert(calcTime('Singapore', '+8'));

// get London time
alert(calcTime('London', '+1'));
});


function calcTime(city, offset) {

// create Date object for current location
d = new Date();

// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);

// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));

// return time as a string
return "The local time in " + city + " is " + nd.toLocaleString();

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

关于javascript - 使用选择选项转换时间。使用 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37046220/

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