使用select选项转换时间.使用javascript

convert time using select option . Using javascript

本文关键字:使用 javascript 时间 选项 select 转换      更新时间:2023-09-26

我在jsfiddle上看到了这个代码http://jsfiddle.net/93pEd/174/.我决定修改项目,因为它有点相似。我添加了另一个下拉菜单作为原点(左)转换为(右)

你们能帮助如何转换时间并将其放置在输入文本字段中吗这是我添加/编辑的代码附言:我没有更改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>