js获取当前日期时间戳格式化日期
日期:2019-12-19
来源:程序思维浏览:2751次

如何让获取到当前时间日期
通过 new Date()对象获取到当前时间日期,因为Date对象用于处理日期与时间。
var date = new Date();//获取当前时间日期 赋值给date 方便后期获取
获取时间戳
接下来通过date对象其中的方法getTime() ,getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。
var time = date.getTime();//当前的毫秒数
var oneDay = 1000*60*60*24;//一天的毫秒数
再做一个简单计算来获取到我们一天所需要的毫秒数赋值给变量。
获取到需要显示在页面上的元素
var app = document.getElementById("app");
app.innerHTML = myGetDate(date);
绑定点击事件
在HTML页面当中给需要点击的按钮绑定点击事件,通过js文件获取到需要点击的按钮做出绑定
//前一天
function goBefore(){
var before = time - oneDay;//计算前一天的毫秒数
date.setTime(before);
app.innerHTML = myGetDate(date);
}
//后一天
function goAfter(){
var after = time + oneDay;//计算前一天的毫秒数
date.setTime(after);
app.innerHTML = myGetDate(date);
}
封装日期格式化
为什么我们需要重新封装日期格式化呢?前段说到我们可以通过Date()对象来获取到当前日期,但是对于其格式却必须进行相应的转换,才能我们想要的格式,所以会用一个简单的方法来封装我们的日期,方便我们的使用。
//封装日期格式化的方法
function myGetDate(d){
return `${d.getFullYear()}年${d.getMonth()+1}月${d.getDate()}日`;
}
// 获取当前时间戳(以s为单位)
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;
//当前时间戳为:1403149534
console.log("当前时间戳为:" + timestamp);
// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;
//2014-07-10 10:21:12的时间戳为:1404958872
console.log(stringTime + "的时间戳为:" + timestamp2);
// 将当前时间换成时间格式字符串
var timestamp3 = 1403058804;
var newDate = new Date();
newDate.setTime(timestamp3 * 1000);
// Wed Jun 18 2014
console.log(newDate.toDateString());
// Wed, 18 Jun 2014 02:33:24 GMT
console.log(newDate.toGMTString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toISOString());
// 2014-06-18T02:33:24.000Z
console.log(newDate.toJSON());
// 2014年6月18日
console.log(newDate.toLocaleDateString());
// 2014年6月18日 上午10:33:24
console.log(newDate.toLocaleString());
// 上午10:33:24
console.log(newDate.toLocaleTimeString());
// Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toString());
// 10:33:24 GMT+0800 (中国标准时间)
console.log(newDate.toTimeString());
// 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'));
时间戳转正常格式
function timestampToTime(timestamp) {
var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds();
return Y+M+D+h+m+s;
}
timestampToTime(1403058804);
console.log(timestampToTime(1403058804));//2014-06-18 10:33:24
正常格式转时间戳的三种方式:
var date = new Date('2014-04-23 18:55:49:123');
// 有三种方式获取
var time1 = date.getTime();
var time2 = date.valueOf();
var time3 = Date.parse(date);
console.log(time1);//1398250549123
console.log(time2);//1398250549123
console.log(time3);//1398250549000
精品好课