教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 将UTC格式时间转换为本地格式时间的js代码

将UTC格式时间转换为本地格式时间的js代码

发布时间:2015-01-25   编辑:jiaochengji.com
本文介绍下,用javascript代码实现将UTC格式时间转换为本地格式时间的方法,有需要的朋友,可以作个参考。

以下代码实现:
将UTC格式时间转换为大家可以看得懂的本地格式时间。

代码:

<script language="javascript">
/*
* UTC格式时间转换本地格式时间
* 整理:www.jbxue.com
**/
Date.prototype.format = function (format) { 
var o = { 
"M+": this.getMonth() + 1, //month 
"d+": this.getDate(), //day 
"h+": this.getHours(), //hour 
"m+": this.getMinutes(), //minute 
"s+": this.getSeconds(), //second 
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
"S": this.getMilliseconds() //millisecond 
} 
if (/(y+)/.test(format)) format = format.replace(RegExp.$1, 
(this.getFullYear() + "").substr(4 - RegExp.$1.length)); 
for (var k in o) if (new RegExp("(" + k + ")").test(format)) 
format = format.replace(RegExp.$1, 
RegExp.$1.length == 1 ? o[k] : 
("00" + o[k]).substr(("" + o[k]).length)); 
return format; 
} 

var TempDate = new Date(); 
TempDate.toLocaleDateString()//2013年9月5日 
TempDate.format("yyyy-MM-dd")//2013-09-05
</script>

您可能感兴趣的文章:
将UTC格式时间转换为本地格式时间的js代码
PHP跨时区(UTC时间)设置的方法
js获取当前时间戳与日期比较
js格式化时间的小例子
js 动态时间显示代码
php时间函数strtotime的深入理解
js字符串日期格式化为yyyy-mm-dd
Javascript时间戳与php时间戳转换时要注意什么
将UTC时间转换为标准时间的代码
python pytz是什么

关键词: 时间转换   
[关闭]
~ ~