// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
console.log("当前时间戳为:" + timestamp); // 获取某个时间格式的时间戳
var stringTime = "2017-12-06 21:51:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2017-12-06 21:51:12的时间戳为:1512568272
console.log(stringTime + "的时间戳为:" + timestamp2); var timestamp3 = 1512567397;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Dec 06 2017
console.log(newDate.toDateString());
// Wed, 06 Dec 2017 13:36:37 GMT
console.log(newDate.toGMTString());
// 2017-12-06T13:36:37.000Z
console.log(newDate.toISOString());
// 2017-12-06T13:36:37.000Z
console.log(newDate.toJSON().replace(/:\d{1,2}$/,''));
// 2017-12-6
console.log(newDate.toLocaleDateString().replace(/\/+/g,'-'));
// 2017/12/6 下午9:36:37
console.log(newDate.toLocaleString());
// 下午9:36:37
console.log(newDate.toLocaleTimeString());
// Wed Dec 06 2017 21:36:37 GMT+0800 (中国标准时间)
console.log(newDate.toString());
// 21:36:37 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());
// Wed, 06 Dec 2017 13:36:37 GMT
console.log(newDate.toUTCString());

  

 var newDate = new Date();
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'));

javascript-时间戳的更多相关文章

  1. 转:javascript时间戳和日期字符串相互转换

    转:javascript时间戳和日期字符串相互转换 <html xmlns="http://www.w3.org/1999/xhtml"> <head> & ...

  2. javascript时间戳和日期字符串相互转换

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  3. JavaScript时间戳与其格式化

    在 PHP + MySQL (日期类型为datetime) + ajax 应用中,有时候需要用 JavaScript 将时间戳类型格式化为一般的时间类型格式.下面提供一些转换的方法,比较常见的一些总结 ...

  4. javascript时间戳与日期格式的相互转换

    这里总结下JavaScript中时间戳和日期格式的相互转换方法(自定义函数). 将时间戳转换为日期格式 function timestampToTime(timestamp) { var date = ...

  5. javascript时间戳与日期格式之间的互转

    1. 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了 ...

  6. Javascript时间戳和日期时间的相互转换

    跟后台对接的时候经常碰到时间格式的问题,有时返回的是时间戳,有时返回的是具体时间,需求又需要它们之间的转换,所以干脆把之前遇到过的情况都给记录下来,以供自己参考! 本文备注:(时间戳单位为毫秒ms,换 ...

  7. javascript时间戳转换成指定格式的日期

    //时间戳转换成指定格式的日期DateTool.IntDatetimeTo = function(time, format){    var testDate = new Date(time);    ...

  8. javascript时间戳转换成yyyy-MM-DD格式

    最近开发中需要和后端进日期和时间传值,前后端约定为时间戳的格式,但是前端展示需要展示成年-月-日的格式.就需要进行日期和时间转换格式.自己总结两个方式就行转换. 一,new Date(时间戳).for ...

  9. [JavaScript] 时间戳格式化为yyyy-MM-dd日期

    function formateDate(timestamp){ var date = new Date(timestamp); var y = 1900+date.getYear(); var m ...

  10. C# DateTime与时间戳转换

    C# DateTime与时间戳的相互转换,包括JavaScript时间戳和Unix的时间戳. 1. 什么是时间戳 首先要清楚JavaScript与Unix的时间戳的区别: JavaScript时间戳: ...

随机推荐

  1. C# to IL 12 Arrays(数组)

    An array is a contiguous block of memory that stores values of the same type. These valuesare an ind ...

  2. .NET本质论 组件

    模块定义 CLR程序存在模块(module)中.一个CLR模块是一个字节流,通常作为一个文件存储在本地的文件系统中或者Web服务器上 CLR模块采用Windows NT的PE/COFF可执行文件格式的 ...

  3. 自动化部署--shell脚本--2

    node1和node2都装apache   [root@linux-node1 ~]# yum install httpd -y Loaded plugins: fastestmirror Loadi ...

  4. 在外网访问家里面的电脑 和 DMZ

    方法1:使用 MDZ (  demilitarized zone), 中文意思 非武装的区域.我们的家用电脑一般都在 路由器所在的 C类内网(192.X.X.X 的 ip).外网是不能直接访问内网的. ...

  5. java-webService(调用wsdl接口)

    使用Axis或XFire实现WebService: Axis2是Apache下的一个重量级WebService框架,准确说它是一个Web Services / SOAP / WSDL 的引擎,是Web ...

  6. kindle电子书下载搜索

    kindle电子书下载搜索 https://sokindle.com/ https://sobooks.cc/

  7. msp430学习笔记-USART

    本文引用:http://bbs.ednchina.com/BLOG_ARTICLE_3013784.HTM MSP430F149有两个USART通讯端口,其性能完全一样,每个通讯口可通过RS232和R ...

  8. C 500uS状态机架构

    main int main(void) { InitSys(); SoftwareInit(); ) { if(P500usReq) { P500usReq = ; P500us(); } Modbu ...

  9. git初始化项目 以及 git常用操作

    初始化项目 $ git config --global user.name "Your Name"  配置用户名 $ git config --global user.email ...

  10. [Android] Surface、SurfaceHolder与SurfaceView

    其实相当于MVC结构的三者关系:M(Surface).V(SurfaceView).C(SurfaceHolder) 1.Surface Handle onto a raw buffer that i ...