js 时间格式与时间戳的相互转换示例代码
更新时间:2013年12月25日 16:25:41 作者:
很多的新手朋友们对js中的时间格式与时间戳的转换比较陌生,下面就为大家详细介绍下具体的转换步骤,感兴趣的朋友可以参考下
一.时间转换时间戳
function transdate(endTime){
var date=new Date();
date.setFullYear(endTime.substring(0,4));
date.setMonth(endTime.substring(5,7)-1);
date.setDate(endTime.substring(8,10));
date.setHours(endTime.substring(11,13));
date.setMinutes(endTime.substring(14,16));
date.setSeconds(endTime.substring(17,19));
return Date.parse(date)/1000;
}
二.时间戳转换时间
(1):转换成 2011-3-16 16:50:43 格式:
function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ")
return tt;
}
(2):转换成 2011年3月16日 16:50:43:
function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString()
return tt;
}
(3):转换成 2011年3月16日 16:50
function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString().substr(0,16);
return tt;
}
复制代码 代码如下:
function transdate(endTime){
var date=new Date();
date.setFullYear(endTime.substring(0,4));
date.setMonth(endTime.substring(5,7)-1);
date.setDate(endTime.substring(8,10));
date.setHours(endTime.substring(11,13));
date.setMinutes(endTime.substring(14,16));
date.setSeconds(endTime.substring(17,19));
return Date.parse(date)/1000;
}
二.时间戳转换时间
(1):转换成 2011-3-16 16:50:43 格式:
复制代码 代码如下:
function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ")
return tt;
}
(2):转换成 2011年3月16日 16:50:43:
复制代码 代码如下:
function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString()
return tt;
}
(3):转换成 2011年3月16日 16:50
复制代码 代码如下:
function getDate(tm){
var tt=new Date(parseInt(tm) * 1000).toLocaleString().substr(0,16);
return tt;
}
相关文章
jscript之Read an Excel Spreadsheet
jscript之Read an Excel Spreadsheet...2007-06-06TypeScript中的类型断言[as语法|<>语法]的使用
本文主要介绍了TypeScript中的类型断言[as语法|<>语法]的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-06-06解析offsetHeight,clientHeight,scrollHeight之间的区别
这篇文章主要是对offsetHeight,clientHeight,scrollHeight之间的区别进行了详细介绍,需要的朋友可以过来参考下,希望对大家有所帮助2013-11-11
最新评论