js时间戳

来源:互联网 发布:vue.js验证手机号码 编辑:程序博客网 时间:2024/06/05 20:49

第一类:::转化为时间戳的形式

//转化方式一   Number()  精确到了毫秒

var newDay = new Date();console.log(Number(newDay));
//转化方式二   Date.parse()  精确到了秒,后三位都是用的0填充的
var newDay = new Date();console.log(Date.parse(newDay));
//转化方式三   转义符进行转义
var newDay = +new Date();console.log(newDay);
//转化方式四   转义符进行转义
var stringTime = "2014-07-10 10:21:12";//2014 07 10 10:21:12  年月日的'-'可带可不带var timestamp2 = Date.parse(new Date(stringTime));console.log(timestamp2);
第二类:::转化为时间

//转化方式一  

var newDate = new Date(1514255532652);console.log(newDate);
//转化方式二 

var timestamp3 = 1514255532652;  //声明一个时间戳var newDate1 = new Date();  //实例化一个Date对象newDate1.setTime(timestamp3); //设置Date对象的时间为时间戳的时间console.log(newDate1)
第三类:::时间戳转化为不同的时间格式

 var timestamp3 = 1514256599688; var newDate = new Date(); 
//3.1。。。 Wed Jun 18 2014格式

console.log(newDate.toDateString());
// 3.2.。。。Wed, 18 Jun 2014 02:33:24 GMT 
console.log(newDate.toGMTString());
//3.3。。。2014-06-18T02:33:24.000Z

console.log(newDate.toISOString());
//3.4。。。2014-06-18T02:33:24.000Z 

console.log(newDate.toJSON())
//3.5.。。。 2014年6月18日

console.log(newDate.toLocaleDateString());
//3.6.。。。2014年6月18日 上午10:33:24

console.log(newDate.toLocaleString());
//3.7.。。。上午10:33:24
console.log(newDate.toLocaleTimeString());
//3.8。。。Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)

console.log(newDate.toString());
//3.9。。。10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());
//4.0。。。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'));











阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 井点降水图片 降水量查询 降温方法 降温 降温神器 手机降温 降温图片 降温软件 防暑降温费 防暑降温 如何降温 降温歌词 降温贴 喷雾降温 喷雾降温降温 怎么降温 工业降温 降温系统 手机降温器 冷雾降温 大棚降温 自动降温器 降温手机壳 降温的方法 降温装置 降温大师 水降温设备 降温风机 物理降温贴 笔记本降温 阴囊降温贴 全国降温 阳光房降温 干冰降温 降温结晶 降温降温 几号降温 宝宝降温贴 排卵降温 风机降温 水喷雾降温