概要:本篇文章主要认识Date,日期时间的方法请点击这里
Date对象的创建、Date对象的方法
创建Date对象:new Date();
Date构造器可以接受各种不同格式的日期输入表示方法,其中包括:
年份
月份:0(1月)到11(12月)
日期:从1到31
时:0到23
分:0到59
秒:0到59
毫秒:0到999
new Date()的写法
第一种,数字的形式:new Date(2016, 5, 13, 18, 35, 8);
第二种,字符串的形式:new Date(‘June 10, 2016 13:12:50’);
控制台输入完整的年、月、日、时、分、秒、毫秒:new Date( 2013, 0, 24, 19, 34, 55, 956 );
如果输入时间超出正常时间以外,Date对象会自动启动溢出式前进处理,
1月到12月的英语单词
一月:January
二月:February
三月:March
四月:April
五月:May
六月:June
七月:July
八月:August
九月:September
十月:October
十一月:November
十二月:December
格式化时间
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'));
获取时间戳
//获取当前时间戳 总结出4种
var nowTime
=new Date();
var nowStamp1
=nowTime
-0;//获取了当前毫秒的时间戳。
var nowStamp2
=Date.parse
(nowTime
);//获取的时间戳是把毫秒改成000显示,
var nowStamp3
=nowTime
.getTime
();//获取了当前毫秒的时间戳。
var nowStamp4
=nowTime
.valueOf
();//获取了当前毫秒的时间戳。
console
.log(nowStamp1
)
console
.log(nowStamp2
)
console
.log(nowStamp3
)
console
.log(nowStamp4
)
//获取指定时间戳 我们要转换的时间 2018-07-07 23:00:00
var targetTime='2018-07-07 23:00:00';
var targetStamp1=new Date(targetTime).getTime();
var targetStamp2=new Date(targetTime).valueOf();
var targetStamp3=new Date(targetTime)-0;
var targetStamp4=Date.parse(new Date(targetTime));
console.log(targetStamp1)//1530975600000 android手机正确转换,但是在ios上就会出现NaN
console.log(targetStamp2)//1530975600000 android手机正确转换,但是在ios上就会出现NaN
console.log(targetStamp3)//1530975600000 android手机正确转换,但是在ios上就会出现NaN
console.log(targetStamp4)//1530975600000 android手机正确转换,但是在ios上就会出现NaN
//获取指定时间戳 我们要转换的时间 2018-07-07 23:00:00
var targetTime='2018/07/07 23:00:00';
var targetStamp1=new Date(targetTime).getTime();
var targetStamp2=new Date(targetTime).valueOf();
var targetStamp3=new Date(targetTime)-0;
var targetStamp4=Date.parse(new Date(targetTime));
console.log(targetStamp1)//1530975600000 这样的格式在ios和android上都可以进行显示
console.log(targetStamp2)//1530975600000 这样的格式在ios和android上都可以进行显示
console.log(targetStamp3)//1530975600000 这样的格式在ios和android上都可以进行显示
console.log(targetStamp4)//1530975600000 这样的格式在ios和android上都可以进行显示
//如果非要用带 - 的时间字符串2018-07-07 23:00:00,也想兼容IOS
//请使用以下格式:
var targetTime='2018-07-07 23:00:00';
var targetStamp1=new Date(targetTime.replace(/-/g, '/')).getTime();
var targetStamp2=new Date(targetTime.replace(/-/g, '/')).valueOf();
var targetStamp3=new Date(targetTime.replace(/-/g, '/'))-0;
var targetStamp4=Date.parse(new Date(targetTime.replace(/-/g, '/')));
console.log(targetStamp1)//1530975600000 这样的格式在ios和android上都可以进行显示
console.log(targetStamp2)//1530975600000 这样的格式在ios和android上都可以进行显示
console.log(targetStamp3)//1530975600000 这样的格式在ios和android上都可以进行显示
console.log(targetStamp4)//1530975600000 这样的格式在ios和android上都可以进行显示
时间戳转日期格式
//指定时间戳转日期格式 1530902499242 => 2018-07-07 02:41:39
function formatDate(stamp){
var now=new Date(stamp)
var y=now.getFullYear();
var m=now.getMonth()+1;
var d=now.getDate();
var hh=now.getHours();
var mm=now.getMinutes();
var ss=now.getSeconds();
return y+'-'+(m<10?'0'+m:m)+'-'+(d<10?'0'+d:d)+' '+(hh<10?'0'+hh:hh)+':'+(mm<10?'0'+mm:mm)+':'+(ss<10?'0'+ss:ss);
}
console.log(formatDate(1530902499242))//2018-07-07 02:41:39
//当前时间戳转日期格式
function formatDate(stamp){
var now=new Date(stamp)
var y=now.getFullYear();
var m=now.getMonth()+1;
var d=now.getDate();
var hh=now.getHours();
var mm=now.getMinutes();
var ss=now.getSeconds();
return y+'-'+(m<10?'0'+m:m)+'-'+(d<10?'0'+d:d)+' '+(hh<10?'0'+hh:hh)+':'+(mm<10?'0'+mm:mm)+':'+(ss<10?'0'+ss:ss);
}
console.log(formatDate(new Date()-0))
console.log('今天是星期'+['日','一','二','三','四','五','六'][new Date().getDay()])
//今天是星期六
倒计时
//指定时间戳换算成的秒数
var targetStamp=new Date('2019-01-01 00:00:00'.replace(/-/g, '/')).getTime()/1000
//当前时间戳换算成的秒数
var nowStamp=Date.parse(new Date())/1000;
//倒计时
console.log('距离2019-01-01 00:00:00还有'+(targetStamp-nowStamp)+'秒')
//倒计时:天时分秒数
setInterval(function(){
var targetStamp=new Date('2018-07-08 06:00:00'.replace(/-/g, '/')).getTime()/1000
var nowStamp=Date.parse(new Date())/1000;
var t=targetStamp-nowStamp
var d=Math.floor(t/86400)<10?'0'+Math.floor(t/86400):Math.floor(t/86400);
var h=Math.floor(t%86400/3600)<10?'0'+Math.floor(t%86400/3600):Math.floor(t%86400/3600);
var m=Math.floor(t%86400%3600/60)<10?'0'+Math.floor(t%86400%3600/60):Math.floor(t%86400%3600/60);
var s=t%60<10?'0'+t%60:t%60;
console.log(d+'天'+h+'时'+m+'分'+s+'秒')
},1000)
倒计时方法:指定时间,还剩的天数+小时数+分钟数+秒数
countTime()
function countTime(){
var timer=setInterval(function(){
var targetStemp=new Date('2018-07-07 04:38:00'.replace(/-/g, '/')).getTime()/1000
var nowStemp=Date.parse(new Date())/1000;
var t=targetStemp-nowStemp
if(t>=0){
var d=Math.floor(t/86400)<10?'0'+Math.floor(t/86400):Math.floor(t/86400);
var h=Math.floor(t%86400/3600)<10?'0'+Math.floor(t%86400/3600):Math.floor(t%86400/3600);
var m=Math.floor(t%86400%3600/60)<10?'0'+Math.floor(t%86400%3600/60):Math.floor(t%86400%3600/60);
var s=t%60<10?'0'+t%60:t%60;
console.log(d+'天'+h+'时'+m+'分'+s+'秒')
}else{
clearInterval(timer)
return false;
}
console.log('*************')
},1000)
}
//clone一份 自执行 避免打开页面出现短暂的空白
(function(){
var targetStemp=new Date('2018-07-07 10:38:00'.replace(/-/g, '/')).getTime()/1000
var nowStemp=Date.parse(new Date())/1000;
var t=targetStemp-nowStemp
if(t>=0){
var d=Math.floor(t/86400)<10?'0'+Math.floor(t/86400):Math.floor(t/86400);
var h=Math.floor(t%86400/3600)<10?'0'+Math.floor(t%86400/3600):Math.floor(t%86400/3600);
var m=Math.floor(t%86400%3600/60)<10?'0'+Math.floor(t%86400%3600/60):Math.floor(t%86400%3600/60);
var s=t%60<10?'0'+t%60:t%60;
document.body.innerHTML=(d+'天'+h+'时'+m+'分'+s+'秒')
}else{
return false;
}
countTime()
})()
倒计时方法
function countTime(){
var timer=setInterval(function(){
var targetStemp=new Date('2018-07-07 10:38:00'.replace(/-/g, '/')).getTime()/1000
var nowStemp=Date.parse(new Date())/1000;
var t=targetStemp-nowStemp
if(t>=0){
var d=Math.floor(t/86400)<10?'0'+Math.floor(t/86400):Math.floor(t/86400);
var h=Math.floor(t%86400/3600)<10?'0'+Math.floor(t%86400/3600):Math.floor(t%86400/3600);
var m=Math.floor(t%86400%3600/60)<10?'0'+Math.floor(t%86400%3600/60):Math.floor(t%86400%3600/60);
var s=t%60<10?'0'+t%60:t%60;
document.body.innerHTML=(d+'天'+h+'时'+m+'分'+s+'秒')
}else{
clearInterval(timer)
return false;
}
console.log('*************')
},1000)
}
Date对象方法
方法 描述
getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。
getFullYear() 从 Date 对象以四位数字返回年份。
getHours() 返回 Date 对象的小时 (0 ~ 23)。
getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
getMonth() 从 Date 对象返回月份 (0 ~ 11)。
getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
getTime() 返回 1970 年 1 月 1 日至今的毫秒数。
getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差。
getUTCDate() 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
getUTCFullYear() 根据世界时从 Date 对象返回四位数的年份。
getUTCHours() 根据世界时返回 Date 对象的小时 (0 ~ 23)。
getUTCMilliseconds() 根据世界时返回 Date 对象的毫秒(0 ~ 999)。
getUTCMinutes() 根据世界时返回 Date 对象的分钟 (0 ~ 59)。
getUTCMonth() 根据世界时从 Date 对象返回月份 (0 ~ 11)。
getUTCSeconds() 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
getYear() 已废弃。 请使用 getFullYear() 方法代替。
parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。
setFullYear() 设置 Date 对象中的年份(四位数字)。
setHours() 设置 Date 对象中的小时 (0 ~ 23)。
setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。
setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。
setMonth() 设置 Date 对象中月份 (0 ~ 11)。
setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。
setTime() setTime() 方法以毫秒设置 Date 对象。
setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
setUTCFullYear() 根据世界时设置 Date 对象中的年份(四位数字)。
setUTCHours() 根据世界时设置 Date 对象中的小时 (0 ~ 23)。
setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)。
setUTCSeconds() setUTCSeconds() 方法用于根据世界时 (UTC) 设置指定时间的秒字段。
setYear() 已废弃。请使用 setFullYear() 方法代替。
toDateString() 把 Date 对象的日期部分转换为字符串。
toGMTString() 已废弃。请使用 toUTCString() 方法代替。
toISOString() 使用 ISO 标准返回字符串的日期格式。
toJSON() 以 JSON 数据格式返回日期字符串。
toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。
toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。
toLocaleString() 据本地时间格式,把 Date 对象转换为字符串。
toString() 把 Date 对象转换为字符串。
toTimeString() 把 Date 对象的时间部分转换为字符串。
toUTCString() 根据世界时,把 Date 对象转换为字符串。
UTC() 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。
valueOf() 返回 Date 对象的原始值。
获取两个时间戳相差多少天
function getDaysByStamps
(stamp1
,stamp2
){
return parseInt
(Math
.abs(stamp1
-stamp2
)/1000/60/60/24)
}
获取两个日期相差多少天
function getDaysByDates
(date1
,date2
){
return parseInt
(Math
.abs(new Date(date1
)-new Date(date2
))/1000/60/60/24)
}
获取半年之前的日期
function getHalfYearDate
(){
var now
=new Date();
var year
=now
.getFullYear
();
var month
=now
.getMonth
()+1;
var d
=now
.getDate();
if(month
-6==0){
var y
=year
-1;
return y
+'-12-'+(d
<10?
'0'+d
:d
)
}else if(month
-6<0){
var y
=year
-1;
var m
=month
-6+12
return y
+'-'+(m
<10?
'0'+m
:m
)+'-'+(d
<10?
'0'+d
:d
)
}else{
return year
+'-0'+(month
-6)+'-'+(d
<10?
'0'+d
:d
)
}
}
console
.log(getHalfYearDate
())
function getHalfYearDate
(t
){
var now
=t
===undefined?
new Date():new Date(t
);
var year
=now
.getFullYear
();
var month
=now
.getMonth
()+1;
var d
=now
.getDate();
if(month
-6==0){
var y
=year
-1;
return y
+'-12-'+(d
<10?
'0'+d
:d
)
}else if(month
-6<0){
var y
=year
-1;
var m
=month
-6+12
return y
+'-'+(m
<10?
'0'+m
:m
)+'-'+(d
<10?
'0'+d
:d
)
}else{
return year
+'-0'+(month
-6)+'-'+(d
<10?
'0'+d
:d
)
}
}
//获取当前时间半年前日期
console
.log(getHalfYearDate
())
//获取2018-06-01半年前日期(指定时间半年前日期)
console
.log(getHalfYearDate
('2018-06-01'))//2017-12-01
/*
getHalfYearDate(t)方法:
不传参:获取当前时间半年前日期
传参: 获取指定时间的半年前日期
*/
function getHalfYearDate
(t
){
var now
=t
===undefined?
new Date():new Date(t
);
var year
=now
.getFullYear
();
var month
=now
.getMonth
()+1;
var d
=now
.getDate();
return month
-6==0?
year
-1+'-12-'+(d
<10?
'0'+d
:d
):month
-6<0?
year
-1+'-'+((month
-6+12)<10?
'0'+(month
-6+12):(month
-6+12))+'-'+(d
<10?
'0'+d
:d
):
year
+'-0'+(month
-6)+'-'+(d
<10?
'0'+d
:d
)
}
//获取当前时间半年前日期
console
.log(getHalfYearDate
())
//获取2018-06-01半年前日期
console
.log(getHalfYearDate
('2018-06-01'))//2017-12-01
console
.log(getHalfYearDate
('2018-01-01'))//2017-07-01
获取一年前的日前
function getOneYearDate
(){
let now
=new Date();
let year
=now
.getFullYear
();
let month
=now
.getMonth
()+1;
let day
=now
.getDate()
return (year
-1)+'-'+(month
<10?
'0'+month
:month
)+'-'+(day
<10?
'0'+day
:day
)
}
获取两个日期之间的所有日期
/*
getAllDates(startDate,endDate)方法:
只传开始日期,返回开始日期到当前日期的所有日期
起止日期都传,返回起止日期之间并包含起止日期在内的所有日期
*/
function getAllDates
(startDate
,endDate
){
var arr
=[];
var now
=new Date();
var endDate
=endDate
===undefined?
(now
.getFullYear
()+'-'+(now
.getMonth
()+1)+'-'+now
.getDate()):endDate
;
var days
=parseInt
(Math
.abs(new Date(startDate
.split(' ')[0]+' 00:00:00') - new Date(endDate
.split(' ')[0]+' 00:00:00'))/1000/24/60/60)+1;
for(var i
=0;i
<days
;i
++){
var stamp
=new Date(new Date(startDate
).getTime
()+i
*86400000);
arr
.push
(stamp
.getFullYear
()+'-'+((stamp
.getMonth
()+1)<10?
'0'+(stamp
.getMonth
()+1):(stamp
.getMonth
()+1))+'-'+(stamp
.getDate()<10?
'0'+stamp
.getDate():stamp
.getDate()))
}
return arr
}
console
.log(getAllDates
('2018-07-01 14:23:44','2018-07-03 14:23:44'))//["2018-07-01", "2018-07-02", "2018-07-03"]
console
.log(getAllDates
('2018-07-01 14:23:44'))//返回2018年7月1号到当前为止所有日期
/*
新增日期颠倒 数据正常返回
*/
function getAllDates
(startDate
,endDate
){
var s
=startDate
,e
=endDate
;
var startDate
,endDate
,arr
=[];
if(endDate
){
if(new Date(startDate
).getTime
()<=new Date(endDate
).getTime
()){
startDate
=s
endDate
=e
}else{
startDate
=e
endDate
=s
}
}else{
var now
=new Date();
startDate
=s
endDate
=now
.getFullYear
()+'-'+(now
.getMonth
()+1)+'-'+now
.getDate()
}
var days
=parseInt
(Math
.abs(new Date(startDate
.split(' ')[0]+' 00:00:00') - new Date(endDate
.split(' ')[0]+' 00:00:00'))/1000/24/60/60)+1;
for(var i
=0;i
<days
;i
++){
var stamp
=new Date(new Date(startDate
).getTime
()+i
*86400000);
arr
.push
(stamp
.getFullYear
()+'-'+((stamp
.getMonth
()+1)<10?
'0'+(stamp
.getMonth
()+1):(stamp
.getMonth
()+1))+'-'+(stamp
.getDate()<10?
'0'+stamp
.getDate():stamp
.getDate()))
}
return arr
}
//日期起止顺序颠倒 正常返回数据
console
.log(getAllDates
('2018/7/03 14:23:44','2018/07/01 14:23:44'))//["2018-07-01", "2018-07-02", "2018-07-03"]
//日期起止顺序正确 正常返回数据
console
.log(getAllDates
('2018/7/03 14:23:44','2018/07/05 14:23:44'))//["2018-07-03", "2018-07-04", "2018-07-05"
//支持 / - 年月日时分秒格式
console
.log(getAllDates
('2018-07-01 14:23:44'))//返回2018年7月1号到当前为止所有日期
//支持 年月日格式
console
.log(getAllDates
('2018-07-01'))//返回2018年7月1号到当前为止所有日期
function getDateTime
(){
var tmpDate
= new Date();
var date = tmpDate
.getDate();
var month
= tmpDate
.getMonth
() + 1 ;
var year
= tmpDate
.getYear
();
year
= tmpDate
.getYear
();
year
= year
>1900 ? year
: year
+1900;
myArray
= new Array(6);
myArray
[0] = "星期日"
myArray
[1] = "星期一"
myArray
[2] = "星期二"
myArray
[3] = "星期三"
myArray
[4] = "星期四"
myArray
[5] = "星期五"
myArray
[6] = "星期六"
weekday
= tmpDate
.getDay
();
return year
+ "年" + month
+ "月" + date + "日 " + myArray
[weekday
] ;
}
function getDateTime
(){
var date=new Date();
year
=date.getFullYear
();
month
=date.getMonth
()+1;
day
=date.getDate();
hh
=date.getHours
();
mm
=date.getMinutes
();
ss
=date.getSeconds
();
mi
=date.getMilliseconds
();
week
='星期'+['日','一','二','三','四','五','六'][date.getDay
()];
var f
=function(n
){return n
<10?
'0'+n
:n
;};
return year
+ '年' + f
(month
) + '月' + f
(day
) + '日 ' + f
(hh
) + ':' + f
(mm
) + ':' + f
(ss
) +' ' + mi
+ ' ' + week
;
}
根据 评论时间 显示不同的状态
function getBetweenDate
(date){
let dt
=new Date();
let yestodayStamp
= dt
.getFullYear
()+'-'+(dt
.getMonth
()+1<10?
'0'+(dt
.getMonth
()+1):(dt
.getMonth
()+1))+'-'+(dt
.getDate()-1)+' 00:00:00'
let todayStamp
= dt
.getFullYear
()+'-'+(dt
.getMonth
()+1<10?
'0'+(dt
.getMonth
()+1):(dt
.getMonth
()+1))+'-'+(dt
.getDate()<10?
'0'+dt
.getDate():dt
.getDate())+' 00:00:00'
let todayEndStamp
= dt
.getFullYear
()+'-'+(dt
.getMonth
()+1<10?
'0'+(dt
.getMonth
()+1):(dt
.getMonth
()+1))+'-'+(dt
.getDate()<10?
'0'+dt
.getDate():dt
.getDate())+' 23:59:59'
if((new Date(date).getTime
())>=new Date(yestodayStamp
).getTime
() && (new Date(date).getTime
())<new Date(todayStamp
).getTime
()){
return date
}else if((new Date(date).getTime
())<new Date(yestodayStamp
).getTime
()){
let diff
= parseInt
((new Date().getTime
() - new Date(date).getTime
())/1000)
if(diff
<30*86400){
return parseInt
(diff
/86400)+'天前';
}else{
return "1个月之前"
}
}else if(new Date(date).getTime
()>=new Date(todayStamp
).getTime
() && new Date(date).getTime
()<new Date(todayEndStamp
).getTime
()){
let diff
= parseInt
((new Date().getTime
() - new Date(date).getTime
())/1000)
if(diff
<60){
return "刚刚"
}else if(diff
<3600){
return parseInt
(diff
/60)+'分钟前'
}else if(diff
<86400){
return parseInt
(diff
/3600)+'小时前'
}
}
}
「两年博客,如果觉得我的文章对您有用,请帮助本站成长」
共有 0 条评论 - JS-Date