关于时间的操作(JavaScript版)——依据不同区时显示对应的时间
如今项目基本上告一段落了,难得有一定的闲暇,今天利用数小时完毕了一个功能模块——依据不同区时显示对应的时间,这方面网上基本没有现成的样例,如今将代码粘贴例如以下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=GB2312" />
<title>依据不同区时显示对应的时间</title>
<script type="text/javascript">
<!-- Begin
function changeTZ() {
var selectedValue = document.getElementById("TZ").value;
var timeZone = selectedValue.split("*")[0];
var city = selectedValue.split("*")[1];
//console.log("所选城市:" + city + "、所选城市区时:" + timeZone); var date = new Date();
var currentTimezone = -(date.getTimezoneOffset()/60);//当前区时
var timeDifference = currentTimezone - timeZone;//时差
console.log("当前区时:"+currentTimezone+"、时差:"+timeDifference); var year = 0; //所选城市时间:年
var month = 0; //所选城市时间:月
var day = 0; //所选城市时间:日
var hour = 0; //所选城市时间:小时
var minute = 0;//所选城市时间:分钟
var second = 0;//所选城市时间:秒 var currentYear = date.getFullYear(); //当前时间:年
var currentMonth = date.getMonth()+1; //当前时间:月
var currentDay = date.getDate(); //当前时间:日
var currentHour = date.getHours(); //当前时间:小时
var currentMinute = date.getMinutes();//当前时间:分钟
var currentSecond = date.getSeconds();//当前时间:秒 second = currentSecond; if(timeDifference.toString().split(".").length == 2){//所选城市区时为小数
var intVlaue = parseInt(timeDifference.toString().split(".")[0]);//获取整数部分
var floatVlaue = timeDifference - intVlaue;//获取小数部分
hour = currentHour- intVlaue;
minute = currentMinute - floatVlaue*60;
if(minute>=60){
hour = hour + 1;
minute = minute - 60;
}else if(minute < 0){
hour = hour - 1;
minute = 60 + minute;
}
//console.log("当前时间(小时):" + currentHour+"、所选城市时间(小时):"+hour);
}else{
hour = currentHour-timeDifference;
minute = currentMinute;
} if(hour >= 24){
day = currentDay + 1;
hour = hour - 24;
}else if(hour >= 0 && hour < 24){
day = currentDay;
hour = hour;
}else if(hour < 0){
day = currentDay - 1;
hour = hour + 24;
} if(currentMonth==1 || currentMonth==3 || currentMonth==5 || currentMonth==7 || currentMonth==8 || currentMonth==10 || currentMonth==12 ){//31天
if(day > 31){
if(currentMonth==12){
year = currentYear + 1;
month= 1;
day = day - 31;
}else{
year = currentYear;
month= currentMonth + 1;
day = day - 31;
}
}else if(day == 0){
if(currentMonth==1){
year = currentYear - 1;
month= 12;
day = 31;
}else{
year = currentYear;
month= currentMonth - 1;
if(month==4 || month==6 || month==9 || month==11){
day = 30;
}else if(month==2){
if((year % 400 == 0)||(year % 4 == 0)&&(year % 100 != 0)){//闰年
day = 29;
}else{//平年
day = 28;
}
}
}
}else{
year = currentYear;
month= currentMonth;
day = day;
}
}
if(currentMonth==4 || currentMonth==6 || currentMonth==9 || currentMonth==11){//30天
if(day > 30){
year = currentYear;
month= currentMonth + 1;
day = day - 30;
}else if(day == 0){
year = currentYear;
month= currentMonth - 1;
day = 31;
}else{
year = currentYear;
month= currentMonth;
day = day;
}
}
if(currentMonth==2){//28天或29天
year = currentYear;
if((year % 400 == 0)||(year % 4 == 0)&&(year % 100 != 0)){//闰年
if(day > 29){
year = currentYear;
month= currentMonth + 1;
day = day - 29;
}else if(day == 0){
year = currentYear;
month= 1;
day = 31;
}else{
year = currentYear;
month= currentMonth;
day = day;
}
}else{//平年
if(day > 28){
year = currentYear;
month= currentMonth + 1;
day = day - 28;
}else if(day == 0){
year = currentYear;
month= 1;
day = 31;
}else{
year = currentYear;
month= currentMonth;
day = day;
}
}
} hour = ((hour <= 9) ? ("0" + hour) : hour);
minute = ((minute <= 9) ? ("0" + minute) : minute);
second = ((second <= 9) ? ("0" + second) : second);
Clock.innerHTML = city + ":" + year +"年"+ month +"月"+ day +"日 " + hour + ":" + minute + ":" + second;
setTimeout("changeTZ()", 1000);
}
// End -->
</script>
</head>
<body bgcolor="#ffffff" onLoad="javascript:changeTZ();">
<div id="Clock"></div>
<select style="font-size: 9pt;" onchange="changeTZ()" id="TZ">
<option value="-12*国际换日线">GMT-12</option>
<option value="-11*萨摩亚群岛">GMT-11</option>
<option value="-10*夏威夷">GMT-10</option>
<option value="-9*阿拉斯加">GMT-9</option>
<option value="-8*太平洋时间">GMT-8</option>
<option value="-7*美国山区">GMT-7</option>
<option value="-6*墨西哥">GMT-6</option>
<option value="-5*南美洲太平洋">GMT-5</option>
<option value="-4.5*加拉加斯">GMT-4.5</option>
<option value="-4*大西洋">GMT-4</option>
<option value="-3.5*纽芬兰">GMT-3.5</option>
<option value="-3*巴西利亚">GMT-3</option>
<option value="-2*大西洋中部">GMT-2</option>
<option value="-1*亚速尔">GMT-1</option>
<option value="0*格林尼治">GMT</option>
<option value="1*罗马">GMT +1</option>
<option value="2*以色列">GMT +2</option>
<option value="3*莫斯科">GMT +3</option>
<option value="3.5*德黑兰">GMT+3.5</option>
<option value="4*巴库">GMT +4</option>
<option value="4.5*喀布尔">GMT+4.5</option>
<option value="5*新德里">GMT +5</option>
<option value="5.5*孟买">GMT+5.5</option>
<option value="5.75*加德满都">GMT+5.75</option>
<option value="6*达卡">GMT +6</option>
<option value="6.5*仰光">GMT+6.5</option>
<option value="7*曼谷">GMT +7</option>
<option value="8*北京" selected>GMT +8</option>
<option value="9*东京">GMT +9</option>
<option value="9.5*达尔文">GMT+9.5</option>
<option value="10*悉尼">GMT +10</option>
<option value="11*马加丹">GMT +11</option>
<option value="12*惠灵顿">GMT +12</option>
</select>
</body>
</html>
【0分下载代码】
关于时间的操作(JavaScript版)——依据不同区时显示对应的时间的更多相关文章
- 解决linux一段时间不操作失去连接的问题
解决mac下ssh空闲一段时间自动断开的问题 http://www.haorooms.com/post/mac_iterm2_ssh 问题现象 用 ssh 命令连接服务器之后,如果一段时间不操作,再次 ...
- JavaScript显示当前时间的操作
JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...
- JavaScript长时间未操作自动退出登录
主要是通过mouseover 来监听有没有进行当前页面操作,通过未操作时间和设定退出的时间做比较,从而退出登录. var oldTime = new Date().getTime(); var new ...
- javascript时间日期操作
Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...
- JavaScript版拼图小游戏
慕课网上准备开个新的jQuery教程,花了3天空闲时间写了一个Javascript版的拼图小游戏,作为新教程配套的分析案例 拼图游戏网上有不少的实现案例了,但是此源码是我自己的实现,所以不做太多的比较 ...
- javascript日历控件——纯javascript版
平时只有下班时间能code,闲来写了个纯javascript版.引用该calendar.js文件,然后给要设置成日历控件的input的id设置成calendar,该input就会变成日历控件. < ...
- WPF程序长时间无人操作
在软件开发中为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面. 判断程序是否长时间无人操作,有两个依据,第一个是鼠标长时间不动,第二个是鼠标 ...
- Web页面长时间无操作后再获取焦点时转到登录界面
今天开始讲新浪博客搬到博客园. 在工作中遇到的小问题,感觉有点意思,就记录下来吧! 该问题分为两种情况,一.Web页面长时间无操作后,在对其进行操作,比如点击“首页”.“设 ...
- Javascript版经典游戏之《扫雷》
翻出年初写的游戏贴上来,扫雷相信大家都玩过,先上图: 源码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
随机推荐
- 【转】char *str 和 char str[]的区别
char str[] = "abcd";定义了一个局部字符数组,返回它的地址肯定是一个已经释放了的空间的地址. 此函数返回的是内部一个局部字符数组str的地址,且函数调用完毕后 此 ...
- CI框架中遇见的一些错误和解决方法 笔记
ps:根据经验不断修改和更新,欢迎指出错误~ 1. An uncaught Exception was encountered Type: Exception Message: Session: Co ...
- Symfony2中的设计模式——装饰者模式
装饰者模式的定义 文章链接:http://www.hcoding.com/?p=101 个人站点:http://www.hcoding.com/ 在不必改变原类文件和使用继承的情况下,动态地扩展一个 ...
- 解决IE10以下对象不支持“bind”属性或方法
IE10一下的浏览器,如果在JS代码中用了bind函数,那么就会报“SCRIPT438: 对象不支持“bind”属性或方法” 因为浏览器没有提供这个参数的方法,所以我们就自己写一个bind,来让这个参 ...
- sql基础复习
--.while循环 declare @sum int declare @i int ) begin set @sum =@sum+@i ) print @i end print @sum --.go ...
- Ubuntu下配置使用maven
下载界面: http://maven.apache.org/download.cgi 当前下载:apache-maven-3.2.5-bin.zip 解压到:/usr/lib/jvm/apache-m ...
- hdu 畅通工程
http://acm.hdu.edu.cn/showproblem.php?pid=1863 #include <cstdio> #include <cstring> #inc ...
- rsyslog的ommsql模块如何连接MYSQL的非标准数据库端口?
搞了我半小个时查找资料..最后,在一个官方文档中找到他... http://www.rsyslog.com/doc/ommysql.html Sample: The following sample ...
- FJ省队集训DAY2 T1
思路:转换成n条三维空间的直线,求最大的集合使得两两有交点. 有两种情况:第一种是以某2条直线为平面,这时候只要统计这个平面上有几条斜率不同的直线就可以了 还有一种是全部交于同一点,这个也只要判断就可 ...
- 中国IC业“芯”结:IC小国真能赶追韩美日么?
集成电路是关系到国民经济和社会发展的战略性.基础性和先导性产业,是培育发展战略性新兴产业.推动信息化和工业化深度融合的核心与基础.因此,我 国历来就十分重视集成电路产业的培育和发展,在这方面投入了大量 ...