博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript时间戳和日期字符串相互转换
阅读量:6913 次
发布时间:2019-06-27

本文共 1915 字,大约阅读时间需要 6 分钟。

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);

 

// 获取某个时间格式的时间戳

var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872
console.log(stringTime + "的时间戳为:" + timestamp2);

 

// 将当前时间换成时间格式字符串

var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Jun 18 2014
console.log(newDate.toDateString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toGMTString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toISOString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toJSON());
// 2014年6月18日
console.log(newDate.toLocaleDateString());
// 2014年6月18日 上午10:33:24
console.log(newDate.toLocaleString());
// 上午10:33:24
console.log(newDate.toLocaleTimeString());
// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toString());
// 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toUTCString());

 

Date.prototype.format = function(format) {

var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
console.log(newDate.format('yyyy-MM-dd h:m:s'));

 

</script>

转载地址:http://bhncl.baihongyu.com/

你可能感兴趣的文章
中国已经过了做手机操作系统的窗口期
查看>>
学生电脑何时能“成为自己”?
查看>>
Provisioning Services 7.6 入门到精通系列之十:自动添加向导
查看>>
创业找投资,你要警惕的三种人---情商培养
查看>>
cisco 2811路由器详细配置
查看>>
烂泥:vcenter5.5无AD下的安装与配置
查看>>
iOS开发那些事--创建基于故事板的iOS 6的HelloWorld
查看>>
理解并取:frame-relay的工作原理
查看>>
MariaDB 10.3支持update多表ORDER BY and LIMIT
查看>>
2015年值得关注的几个WEB技术
查看>>
Goroutine(协程)为何能处理大并发?
查看>>
QTP中VBS脚本下FSO、WSH的应用——实例讲解
查看>>
乐视超级手机的跨界影响
查看>>
A dream and two wheels
查看>>
我花50元赚来190元的贴吧推广经验
查看>>
指针和引用
查看>>
Python应用01 原始Python服务器
查看>>
Android应用程序与SurfaceFlinger服务的关系概述和学习计划
查看>>
WCF WinCE 中 手机端 非字符串型 datetime,int,decimal,double 等等 传递不到WCF端的解决方案...
查看>>
iis6.0 之前版本发布silverlight程序 注意事项
查看>>