如今项目基本上告一段落了,难得有一定的闲暇,今天利用数小时完毕了一个功能模块——依据不同区时显示对应的时间,这方面网上基本没有现成的样例,如今将代码粘贴例如以下:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=GB2312" />
  5. <title>依据不同区时显示对应的时间</title>
  6. <script type="text/javascript">
  7. <!-- Begin
  8. function changeTZ() {
  9. var selectedValue = document.getElementById("TZ").value;
  10. var timeZone = selectedValue.split("*")[0];
  11. var city = selectedValue.split("*")[1];
  12. //console.log("所选城市:" + city + "、所选城市区时:" + timeZone);
  13.  
  14. var date = new Date();
  15. var currentTimezone = -(date.getTimezoneOffset()/60);//当前区时
  16. var timeDifference = currentTimezone - timeZone;//时差
  17. console.log("当前区时:"+currentTimezone+"、时差:"+timeDifference);
  18.  
  19. var year = 0; //所选城市时间:年
  20. var month = 0; //所选城市时间:月
  21. var day = 0; //所选城市时间:日
  22. var hour = 0; //所选城市时间:小时
  23. var minute = 0;//所选城市时间:分钟
  24. var second = 0;//所选城市时间:秒
  25.  
  26. var currentYear = date.getFullYear(); //当前时间:年
  27. var currentMonth = date.getMonth()+1; //当前时间:月
  28. var currentDay = date.getDate(); //当前时间:日
  29. var currentHour = date.getHours(); //当前时间:小时
  30. var currentMinute = date.getMinutes();//当前时间:分钟
  31. var currentSecond = date.getSeconds();//当前时间:秒
  32.  
  33. second = currentSecond;
  34.  
  35. if(timeDifference.toString().split(".").length == 2){//所选城市区时为小数
  36. var intVlaue = parseInt(timeDifference.toString().split(".")[0]);//获取整数部分
  37. var floatVlaue = timeDifference - intVlaue;//获取小数部分
  38. hour = currentHour- intVlaue;
  39. minute = currentMinute - floatVlaue*60;
  40. if(minute>=60){
  41. hour = hour + 1;
  42. minute = minute - 60;
  43. }else if(minute < 0){
  44. hour = hour - 1;
  45. minute = 60 + minute;
  46. }
  47. //console.log("当前时间(小时):" + currentHour+"、所选城市时间(小时):"+hour);
  48. }else{
  49. hour = currentHour-timeDifference;
  50. minute = currentMinute;
  51. }
  52.  
  53. if(hour >= 24){
  54. day = currentDay + 1;
  55. hour = hour - 24;
  56. }else if(hour >= 0 && hour < 24){
  57. day = currentDay;
  58. hour = hour;
  59. }else if(hour < 0){
  60. day = currentDay - 1;
  61. hour = hour + 24;
  62. }
  63.  
  64. if(currentMonth==1 || currentMonth==3 || currentMonth==5 || currentMonth==7 || currentMonth==8 || currentMonth==10 || currentMonth==12 ){//31天
  65. if(day > 31){
  66. if(currentMonth==12){
  67. year = currentYear + 1;
  68. month= 1;
  69. day = day - 31;
  70. }else{
  71. year = currentYear;
  72. month= currentMonth + 1;
  73. day = day - 31;
  74. }
  75. }else if(day == 0){
  76. if(currentMonth==1){
  77. year = currentYear - 1;
  78. month= 12;
  79. day = 31;
  80. }else{
  81. year = currentYear;
  82. month= currentMonth - 1;
  83. if(month==4 || month==6 || month==9 || month==11){
  84. day = 30;
  85. }else if(month==2){
  86. if((year % 400 == 0)||(year % 4 == 0)&&(year % 100 != 0)){//闰年
  87. day = 29;
  88. }else{//平年
  89. day = 28;
  90. }
  91. }
  92. }
  93. }else{
  94. year = currentYear;
  95. month= currentMonth;
  96. day = day;
  97. }
  98. }
  99. if(currentMonth==4 || currentMonth==6 || currentMonth==9 || currentMonth==11){//30天
  100. if(day > 30){
  101. year = currentYear;
  102. month= currentMonth + 1;
  103. day = day - 30;
  104. }else if(day == 0){
  105. year = currentYear;
  106. month= currentMonth - 1;
  107. day = 31;
  108. }else{
  109. year = currentYear;
  110. month= currentMonth;
  111. day = day;
  112. }
  113. }
  114. if(currentMonth==2){//28天或29天
  115. year = currentYear;
  116. if((year % 400 == 0)||(year % 4 == 0)&&(year % 100 != 0)){//闰年
  117. if(day > 29){
  118. year = currentYear;
  119. month= currentMonth + 1;
  120. day = day - 29;
  121. }else if(day == 0){
  122. year = currentYear;
  123. month= 1;
  124. day = 31;
  125. }else{
  126. year = currentYear;
  127. month= currentMonth;
  128. day = day;
  129. }
  130. }else{//平年
  131. if(day > 28){
  132. year = currentYear;
  133. month= currentMonth + 1;
  134. day = day - 28;
  135. }else if(day == 0){
  136. year = currentYear;
  137. month= 1;
  138. day = 31;
  139. }else{
  140. year = currentYear;
  141. month= currentMonth;
  142. day = day;
  143. }
  144. }
  145. }
  146.  
  147. hour = ((hour <= 9) ? ("0" + hour) : hour);
  148. minute = ((minute <= 9) ? ("0" + minute) : minute);
  149. second = ((second <= 9) ? ("0" + second) : second);
  150. Clock.innerHTML = city + ":" + year +"年"+ month +"月"+ day +"日 " + hour + ":" + minute + ":" + second;
  151. setTimeout("changeTZ()", 1000);
  152. }
  153. // End -->
  154. </script>
  155. </head>
  156. <body bgcolor="#ffffff" onLoad="javascript:changeTZ();">
  157. <div id="Clock"></div>
  158. <select style="font-size: 9pt;" onchange="changeTZ()" id="TZ">
  159. <option value="-12*国际换日线">GMT-12</option>
  160. <option value="-11*萨摩亚群岛">GMT-11</option>
  161. <option value="-10*夏威夷">GMT-10</option>
  162. <option value="-9*阿拉斯加">GMT-9</option>
  163. <option value="-8*太平洋时间">GMT-8</option>
  164. <option value="-7*美国山区">GMT-7</option>
  165. <option value="-6*墨西哥">GMT-6</option>
  166. <option value="-5*南美洲太平洋">GMT-5</option>
  167. <option value="-4.5*加拉加斯">GMT-4.5</option>
  168. <option value="-4*大西洋">GMT-4</option>
  169. <option value="-3.5*纽芬兰">GMT-3.5</option>
  170. <option value="-3*巴西利亚">GMT-3</option>
  171. <option value="-2*大西洋中部">GMT-2</option>
  172. <option value="-1*亚速尔">GMT-1</option>
  173. <option value="0*格林尼治">GMT</option>
  174. <option value="1*罗马">GMT +1</option>
  175. <option value="2*以色列">GMT +2</option>
  176. <option value="3*莫斯科">GMT +3</option>
  177. <option value="3.5*德黑兰">GMT+3.5</option>
  178. <option value="4*巴库">GMT +4</option>
  179. <option value="4.5*喀布尔">GMT+4.5</option>
  180. <option value="5*新德里">GMT +5</option>
  181. <option value="5.5*孟买">GMT+5.5</option>
  182. <option value="5.75*加德满都">GMT+5.75</option>
  183. <option value="6*达卡">GMT +6</option>
  184. <option value="6.5*仰光">GMT+6.5</option>
  185. <option value="7*曼谷">GMT +7</option>
  186. <option value="8*北京" selected>GMT +8</option>
  187. <option value="9*东京">GMT +9</option>
  188. <option value="9.5*达尔文">GMT+9.5</option>
  189. <option value="10*悉尼">GMT +10</option>
  190. <option value="11*马加丹">GMT +11</option>
  191. <option value="12*惠灵顿">GMT +12</option>
  192. </select>
  193. </body>
  194. </html>

0分下载代码

关于时间的操作(JavaScript版)——依据不同区时显示对应的时间的更多相关文章

  1. 解决linux一段时间不操作失去连接的问题

    解决mac下ssh空闲一段时间自动断开的问题 http://www.haorooms.com/post/mac_iterm2_ssh 问题现象 用 ssh 命令连接服务器之后,如果一段时间不操作,再次 ...

  2. JavaScript显示当前时间的操作

    JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...

  3. JavaScript长时间未操作自动退出登录

    主要是通过mouseover 来监听有没有进行当前页面操作,通过未操作时间和设定退出的时间做比较,从而退出登录. var oldTime = new Date().getTime(); var new ...

  4. javascript时间日期操作

    Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();   ...

  5. JavaScript版拼图小游戏

    慕课网上准备开个新的jQuery教程,花了3天空闲时间写了一个Javascript版的拼图小游戏,作为新教程配套的分析案例 拼图游戏网上有不少的实现案例了,但是此源码是我自己的实现,所以不做太多的比较 ...

  6. javascript日历控件——纯javascript版

    平时只有下班时间能code,闲来写了个纯javascript版.引用该calendar.js文件,然后给要设置成日历控件的input的id设置成calendar,该input就会变成日历控件. < ...

  7. WPF程序长时间无人操作

    在软件开发中为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面. 判断程序是否长时间无人操作,有两个依据,第一个是鼠标长时间不动,第二个是鼠标 ...

  8. Web页面长时间无操作后再获取焦点时转到登录界面

    今天开始讲新浪博客搬到博客园.        在工作中遇到的小问题,感觉有点意思,就记录下来吧!        该问题分为两种情况,一.Web页面长时间无操作后,在对其进行操作,比如点击“首页”.“设 ...

  9. Javascript版经典游戏之《扫雷》

    翻出年初写的游戏贴上来,扫雷相信大家都玩过,先上图: 源码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

随机推荐

  1. mysql存储过程、函数和触发器的创建 [转]

    http://blog.itpub.net/559237/viewspace-438942/ 今天花了半天时间来研究mysql的存储过程函数和触发器的创建,觉得和oracle的大同小异,只是语法上更艰 ...

  2. 使用AudioTrack播放PCM音频数据(android)

    众所周知,Android的MediaPlayer包含了Audio和video的播放功能,在Android的界面上,Music和Video两个应用程序都是调用MediaPlayer实现的.MediaPl ...

  3. jquery之checkbox

    //checkbox 数据回显 var publishRange=rowData.publishRange.split(","); for(var i = 0;i < pub ...

  4. Android WiFiDirect 学习(二)——Service Discovery

    Service Discovery 简介 在Android WifiDirect学习(一 )中,简单介绍了如何使用WifiDirect进行搜索——连接——传输. 这样会有一个问题,那就是你会搜索到到附 ...

  5. ORACLE基本SQL语句-添加更新数据函数篇

    一.添加数据 /*添加数据*/insert into STU values('stu0004','赵一',18,1,"kc0004");insert into STU(STU_ID ...

  6. Git学习笔记--Git常用命令

    参考资料: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 mkdir myfir ...

  7. iOS之UISearchBar实时显示结果

    iOS之UISearchBar实时显示结果     UISearchBar 经常是配合UITableView 一起使用的,一般都将UITableView的tableHeaderView属性设置为UIS ...

  8. matlab读取多幅图片,并对读取的图片降采样和双三次插值

    clear all clc im = {}; %%创建字典im以保存读取的图片 dis = dir('C:\Users\KCl\Documents\MATLAB\SRCNN\Set5\*.bmp'); ...

  9. jquery.mmenu

    http://mmenu.frebsite.nl/ 左右滑动效果 http://blog.sina.com.cn/s/blog_6a0a183f0100zsfk.html js的左右滑动触屏事件,主要 ...

  10. poj 3792 Area of Polycubes

    http://poj.org/problem?id=3792 #include <cstdio> #include <cstring> #include <cmath&g ...