// 获取当前时间戳(以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. Hi3518EV200音频相关

    1.sample程序可以录音,音频格式为G711A.G711U.G726.ADPCM: 2.ADPCM找不到音频播放器 3.G711格式海思添加了4字节头0x00 0x01 0x0a 0x00,普通播 ...

  2. 推荐一个 基于 WebSocket 和 Redis 的 即时通信 开源项目

    项目地址 : https://github.com/2881099/im 大家可以和 SignalR 比较看看 ,  如何  ?        ^^  ^^  ^^ 这是一个 网友 写的 , 他还写了 ...

  3. Qt开发问答

    Qt开发问答 1, Difference between Dialog and widget and QMainWindow http://www.qtcentre.org/threads/3465- ...

  4. [转]linux中vim命令

    在vi中按u可以撤销一次操作 u      撤销上一步的操作 ctrl+r 恢复上一步被撤销的操作 在vi中移动光标至: 行首:^或0 行尾:$ 页首:1G(或gg) 页尾:G(即shift+g) 显 ...

  5. 关于Bagging

    Bagging分为两种:Bagging和Pasting,前者是概率中的放回随机采样,后者是不放回随机采样:默认是放回采样随机:设置bootstrap=False即设置为不放回采样:默认bootstra ...

  6. Ansible 常用模块之ping(四)

    一.ping 模块 1.用途: 测试主机之间的连通性: 2.关键字:ping 3.参数:无 4.用法: ansible all -m ping 命令简单,测试所有服务器是否与控制机网络连通:

  7. 深入理解ASP.NET MVC(3)

    系列目录 URL是如何通过路由表生成的(outbound) 通常我们被推荐在view设计时使用Html.ActionLink(…)产生链接,这样做的优势就是,url可以根据路由表生成.路由机制的另一个 ...

  8. go thrift报错问题--WriteStructEnd

    问题 go thrift开发过程中,多个goroutine共用一个client时,报错: panic: runtime error: index out of range goroutine 24 [ ...

  9. 基于Elasticsearch的智能客服机器人

    本次分享主要会介绍一下ES是如何帮我们完成NLP的任务的.在做NLP相关任务的时候,ES的相似度算法并不足以支撑用户的搜索,需要使用一些与语义相关的方法进行改进.但是ES的很多特性对我们优化搜索体验是 ...

  10. 【转】mysql给root开启远程访问权限,修改root密码

    好记性不如烂笔头,偶然用一直忘.... mysql给root开启远程访问权限,修改root密码   1.MySql-Server 出于安全方面考虑只允许本机(localhost, 127.0.0.1) ...