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'));
阅读全文