// 获取当前时间戳(以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'));

js 时间戳和日期互转的更多相关文章

  1. .net 时间戳和日期互转 【转】http://www.cnblogs.com/zhuiyi/p/5307540.html

    .net 时间戳和日期互转 1.时间戳转日期public static DateTime IntToDateTime(int timestamp){ return TimeZone.CurrentTi ...

  2. js 时间戳转为日期格式

    原文:js 时间戳转为日期格式 js 时间戳转为日期格式 什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time) ...

  3. js时间戳转换日期

    //js时间戳转换日期function formatDate(now) { var year=now.getFullYear(); var month=now.getMonth()+1; var da ...

  4. js时间戳与日期格式之间相互转换

    ###js时间戳与日期格式之间相互转换 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法, ...

  5. js时间戳转为日期函数

    js时间戳转为日期函数 function add0(m){ return m<10?'0'+m:m; } //timestamp参数示例:1501234567 function format(t ...

  6. js时间戳与日期格式的相互转换

    下面总结一下js中时间戳与日期格式的相互转换: 1. 将时间戳转换成日期格式: function timestampToTime(timestamp) { var date = new Date(ti ...

  7. .net 时间戳和日期互转

    1.时间戳转日期 public static DateTime IntToDateTime(int timestamp) {     ,,)).AddSeconds(timestamp); } 调用: ...

  8. PHP时间戳和日期互转换

    在php中我们要把时间戳转换日期可以直接使用date函数来实现,如果要把日期转换成时间戳可以使用strtotime()函数实现,下面我来给大家举例说明. 1.php中时间转换函数 strtotime ...

  9. js时间戳转日期

    //时间戳转日期 2017-04-30 13:20 //type=1--> 2017-04-30 13:20 //type=2-->2018年08月 //type=3-->2018- ...

随机推荐

  1. 3.4 C++名字隐藏

    参数:http://www.weixueyuan.net/view/6361.html 总结: 如果派生类中新增一个成员变量,该成员变量与基类中的成员变量同名,则新增的成员变量就会遮蔽从基类中继承过来 ...

  2. DevExpress WPF v18.2新版亮点(三)

    买 DevExpress Universal Subscription  免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...

  3. ssm使用双数据源

    工作中需要接入其他公司业务系统的数据进行分析,于是接入它们的db. 使用双数据源配置感觉如下: database.sessionFactory.扫描器.事务管理器等双份. 听说如果两个数据源需要一起使 ...

  4. a recipe kindly provided by Dimas for kikuchi

    https://sianipar17.com/2017/12/14/tutorial-for-teleseismic-body-wave-inversion-program/

  5. <Spark><Programming><Key/Value Pairs><RDD>

    Working with key/value Pairs Motivation Pair RDDs are a useful building block in many programs, as t ...

  6. 简单的SpringWebFlow例子及遇到的问题

    这段时间在看<Spring 实战>里面有讲Spring Web Flow,觉得里面的例子过于复杂,不适合新手,于是在网上找了个例子,跟着写 以下是项目的目录,我是基于maven搭建项目的 ...

  7. Power BI十大视觉效果,知多少?

    Power BI十大视觉效果: 图表滑块 响应式布局 一键更改方差计算 一键排序 我们的最爱:小倍数! 瀑布图中的轴断裂 层次表(完全响应) HICHERT博士的IBCS®原生支持 分段图表 - 可视 ...

  8. dubbo 框架文档地址

    http://dubbo.apache.org/books/dubbo-dev-book/ http://dubbo.apache.org/books/dubbo-admin-book/ http:/ ...

  9. Prepare paddle in Docker

    1. Install Docker sudo apt-get install -y docker.io a) pull repository from server in China, here is ...

  10. Linux内核info leak漏洞

    1  Information Leak漏洞风险 从应用层软件,到hypervisor再到kernel代码,都存在Information Leak的风险.下面给出一些示例: 应用层软件:通常是应用敏感数 ...