转换时间戳
1 var oDate = new Date(); //实例一个时间对象; 2 oDate.getFullYear(); //获取系统的年; 3 oDate.getMonth()+1; //获取系统月份,由于月份是从0开始计算,所以要加1 4 oDate.getDate(); // 获取系统日, 5 oDate.getHours(); //获取系统时, 6 oDate.getMinutes(); //分 7 oDate.getSeconds(); //秒 8 let date = child.ymd; 9 let time = `${oDate.getFullYear()}-${oDate.getMonth()+1}-${oDate.getDate()}`//拼接当前时间 年月日 10 console.log(Date.parse(new Date(time)),'当前时间') //转换时间戳 11 console.log(Date.parse(new Date(date)),"点击时间");
一:时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(new Date()); // 结果:1477808630000 不推荐这种办法,毫秒级别的数值被转化为000 console.log(timestamp1); 2.var timestamp2 = (new Date()).valueOf(); // 结果:1477808630404 通过valueOf()函数返回指定对象的原始值获得准确的时间戳值 console.log(timestamp2); 3.var timestamp3 = new Date().getTime(); // 结果:1477808630404 ,通过原型方法直接获得当前时间的毫秒值,准确 console.log(timestamp3); 4.var timetamp4 = Number(new Date()) ; //结果:1477808630404 ,将时间转化为一个number类型的数值,即时间戳 console.log(timetamp4);
function getdate() { var now = new Date(), y = now.getFullYear(), m = now.getMonth() + 1, d = now.getDate(); return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8); }
//将当前的时间转换成时间戳 var now = new Date(); console.log(now.getTime()); //将指定的日期转换成时间戳 var t = "2018-07-23 20:5:30"; var T = new Date(t); console.log(T.getTime()); //console.log(),为控制台打印
// 获取当前时间戳(以s为单位) var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; //当前时间戳为:1403149534 console.log("当前时间戳为:" + timestamp); // 获取某个时间格式的时间戳 var stringTime = "2018-07-10 10:21:12"; var timestamp2 = Date.parse(new Date(stringTime)); timestamp2 = timestamp2 / 1000; //2018-07-10 10:21:12的时间戳为:1531189272 console.log(stringTime + "的时间戳为:" + timestamp2); // 将当前时间换成时间格式字符串 var timestamp3 = 1531189272; var newDate = new Date(); newDate.setTime(timestamp3 * 1000); // Wed Jun 18 2018 console.log(newDate.toDateString()); // Wed, 18 Jun 2018 02:33:24 GMT console.log(newDate.toGMTString()); // 2018-06-18T02:33:24.000Z console.log(newDate.toISOString()); // 2018-06-18T02:33:24.000Z console.log(newDate.toJSON()); // 2018年6月18日 console.log(newDate.toLocaleDateString()); // 2018年6月18日 上午10:33:24 console.log(newDate.toLocaleString()); // 上午10:33:24 console.log(newDate.toLocaleTimeString()); // Wed Jun 18 2018 10:33:24 GMT+0800 (中国标准时间) console.log(newDate.toString()); // 10:33:24 GMT+0800 (中国标准时间) console.log(newDate.toTimeString()); // Wed, 18 Jun 2018 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'));
Sometimes you need to applaud yourself