【发布时间】:2014-11-22 07:56:26
【问题描述】:
我敢打赌,肯定有一些插件可以把这个问题变成更容易解决的问题,比如 MomentJs,但事实是;我不知道如何使用它:) 所以我想尽可能地做它。
我已经构建了一个几乎可以在此处实现的目标的示例:
http://jsfiddle.net/Frondor/5a5wrssn/
界面和控件已经制作完成,但我仍然缺少以下功能: 1. 自动检测用户的时区(如“-1”、“0”、“+2”)并计算给定时间(见输入#time) 以匹配法国时区(UTC/GMT +2)。
以某种顺序:
- 获取用户当前时间/时区(仅小时,而不是分钟或秒)
- 获取用户介绍的计算Calculate的时间
- 引入了其他国家/地区的时间(在本例中为法国)。
- 以小时形式返回值,例如 02:00
到目前为止,这是我的脚本,这些主要是控件,我不知道如何构建所需的函数才能像我上面描述的那样工作:)
$(document).ready(function () {
//Automatically detect user's timezone and store it inside this variable
//Let's use a temporal example just to see how the whole script works
var userTimeZone = "-3"; //here goes a function, its just an example of the variable userTimeZone already resolved for some function I don't know how to code hehe
$("#detected").hide().fadeIn(2000); //just4fancy
$("#uTZ").text(userTimeZone); //print the detected timezone
var timeInput = $('#time');
timeInput.focus();
timeInput.on('change keyup mouseup', function () {
var userTZ = $("#uTZ").text();
var franceTZ = "2"; //predefined timezone of France +2
//Now I try to set the differnce of timezones in order to see how to calculate the final time
var calculation = userTZ-franceTZ; //here is where the calculation happens. Actually, It is (of course) wrong :)
var actualValue = $(this).val();
//print the results
if (timeInput.val() !== '00') {
$('#actual').text(actualValue + ':00 hours');
$('#result').text(actualValue - calculation + ':00 hours'); //this calculates
the given time with the difference of timezones to show a converted time
$('.panel-footer').removeClass('hidden').hide().slideDown();
}
}).bind('mousewheel', function (e) {
return false;
});
});
如果它对某人有用,我发现了这个Convert timezone offset number of hours to timezone offset in (military?) hours,但我不知道如何在我的脚本上实现它:(
提前致谢!
【问题讨论】:
-
我鼓励您阅读我写的内容,尤其是第一段。
标签: javascript jquery datetime timezone timezone-offset