专题 | Date 日期

日期用于处理日期和时间。

Sunday 星期天 Sun

Monday 星期一 Mon           Tuesday 星期二 Tue

Wednesday 星期三 Wed     Thursday 星期四 Thur

Friday 星期五 Fri                 Saturday 星期六 Sat

January 一月 Jan              February 二月 Feb            March 三月 Mar

April   四月 Apr                  May  五月 May                 June 六月  Jun

July  七月  Jul                    August 八月 Aug              September 九月 Sep

October 十月 Oct              November 十一月 Nov      December 十二月 Dec 

Date对象

使用日期函数前需要先创建Date对象,创建了这个对象才能使用Date对象带有的函数,比如getmonth()

var d=new Date();

获取当前时间

getFullYear()  获取年份                       getMonth获取月份

getDay() 返回一周内的某一天            getDate() 返回一个月内的某一天

getTime()  返回距 1970 年 1 月 1 日之间的毫秒数

例子: 获取到现在的年份、月份、天数

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
		
		var d=new Date();
		console.log(d);

        //获取年份getFullYear();
		console.log(d.getFullYear());//2021
		
		
		//获取月份getMonth 月份是0-11
		var g=new Date();
		console.log(g.getMonth());//6   输出6意味着现在7月
		
		//getDay()返回一周内的某一天  0-6 
		var c=new Date();
		// 今天是2021.7.26 是星期一
		console.log(c.getDay());//1 
		
		
		//getDate()返回一个月内的某一天
		var b=new Date();
		console.log(b.getDate());//26
		
		
		// 综合练习:返回某年某月某日并打印出来。
		var obj=new Date();
		var year=obj.getFullYear();
		var month=obj.getMonth()+1;
		var day=obj.getDate();
	    var day2=obj.getDay();
		console.log(year +"年"+ month+"月"+day+"日"+"星期"+day2)
		</script>
	</body>
</html>

 例子:获取到现在的毫秒数

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
     var d=new Date();
	 console.log(d.getTime());
		</script>
	</body>
</html>

 

特殊的月份和星期几

getMonth()方法可以返回表示月份的数字。返回值是0代表一月,11代表12月

getDay()方法返回一周里的某一天。返回值0代表星期天 ,一代表星期一,六代表星期六

例子:注意获取到的月份值有两种,一种是几月份就是几月份,一种是当前月份减-1

不能理解为啥有些是6月有些是7月

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
		// 现在是2021年7月26号
		var d=new Date();
		console.log(d);//7月
		
		var month=new Array();
		month[0]="january";//一月
		month[1]="February";//二月
		month[2]="March";//三月
		month[3]="April";//四月
		month[4]="May";//五月
		month[5]="june";//六月
		month[6]="july";//七月
		month[7]="August";//八月
		month[8]="September";//九月
		month[9]="October";//十月
		month[10]="Nobermber";//十一月
		month[11]="December";//十二月
		console.log(month[d.getMonth()]);//july 7月
		
		console.log(d.getMonth());//6月
		</script>
	</body>
</html>

例子:注意获取到的周几值

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
		// 现在是2021年7月26号星期一
		var d=new Date();
		console.log(d);//星期一
		
		var weekday=new Array(7);
	    weekday[0]="周日/星期天";
		weekday[1]="周一/星期一";
		weekday[2]="周二/星期二";
		weekday[3]="周三/星期三";
		weekday[4]="周四/星期四";
		weekday[5]="周五/星期五";
		weekday[6]="周六/星期六";
		console.log(weekday[d.getDay()]);//周一/星期一
		
		console.log(d.getDay());//1
		</script>
	</body>
</html>

设置具体时间

例子:new Date需要加引号,set不需要加引号

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
		// 今天是2021年7月26号 星期一
		
    	var d=new Date();
	    console.log(d);//Mon Jul 26 2021 10:44:26 GMT+0800 (中国标准时间)
		               //星期一  七月
	
	    var d=new Date("2021,7,26");
	     console.log(d);//Mon Jul 26 2021 00:00:00 GMT+0800 (中国标准时间)
		 
		 //注意:当忘了加引号时出现的值会错
		var d=new Date(2021,7,26);
		console.log(d);//Thu Aug 26 2021 00:00:00 GMT+0800 (中国标准时间)
		               //星期四 八月
		
		//这是正确写法
		var c=d.setFullYear(2021,7,26);
		console.log(c);//1629907200000
		
		//错误写法
		var e=d.setFullYear("2021,7,26");
		console.log(e);//NaN
		
		//错误写法
		var f=d.setFullYear("2021,7,26");
		console.log(f);//NaN
		</script>
	</body>
</html>

例子:星期几的几是数字还是汉字

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
	//取出数组中
	//今天是2021/8/3 星期二
	var d=new Date();
	var week=["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
	console.log("今天是"+week[d.getDay()]);
		</script>
	</body>
</html>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
	
	//今天是2021/8/3 星期二
	var d=new Date();
	console.log("今天星期"+d.getDay());
		</script>
	</body>
</html>

 时间戳

案列:获取时间戳精确到秒

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			// 获取当前时间
			var time=new Date();
			console.log(time);
			
			// 获取当前秒的时间戳
			var time2=Date.parse(new Date());
			console.log(time2,"time2");
			
			
		</script>
	</body>
</html>

 

案列:获取时间戳精确到毫秒

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			// 获取当前时间,这段不写不影响
			var time=new Date();
			console.log(time);
			
			// 获取当前毫秒的时间戳
			var time2=(new Date()).valueOf();
			console.log(time2,"time2");
			
			//获取当前毫秒的时间戳
			var time3=(new Date()).getTime();
			console.log(time3,"time3");
		</script>
	</body>
</html>

 

时间转换

日期格式转换

例子:日期格式转换

第四个输出不用看

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			var time=new Date();
			var time1=new Date(2000,1,21);
			console.log(time,"time");
			console.log(time1,"time1");
			// 转换成现在日期
			console.log(time.toLocaleString(),"time");
			console.log(time1.getFullYear(),"time1");
		</script>
	</body>
</html>

例子:时间戳转换为日期

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>	
		//方法一 时间戳转换为日期
		var time=(new Date()).getTime();
		var	period=new Date(time);
		console.log(period.toLocaleString(),"time");
		
		
		//方法二  时间戳转换为日期
		var time2=(new Date()).getTime();
		function timestamp(now){
			var year=now.getFullYear();
			var month=now.getMonth()+1;
			var date=now.getDate();
			var hour=now.getHours();
			var minute=now.getMinutes();
			var second=now.getMinutes();
			return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second
		}
		var cute=new Date(time2);
		console.log(timestamp(cute),"time2");
		</script>
	</body>
</html>

 

 

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>	
		//不用函数写法
		  var time2=(new Date()).getTime();
		
		  var now=new Date(time2);
		  var year=now.getFullYear();
		  var month=now.getMonth()+1;
		  var date=now.getDate();
		  var hour=now.getHours();
		  var minute=now.getMinutes();
		  var second=now.getMinutes();
		  var cd= year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second
	
	    console.log(cd);
		</script>
	</body>
</html>

 

例子:日期数字和汉字的转换

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
		var name="Kevin";
		var sex=1;
		var price=12.5;
		var count=5;
		var orderTime=1607304532851;
		var arr=["日","一","二","三","四","五","六"];
		
		// 根据上面提供的信息输出内容
		console.log(`Welcome ${name}`);
		console.log(`性别:${sex==1?"男":"女"}`);
		console.log(`
		单价:¥${price.toFixed(2)}
		数量:${count}
		============================
		总价:¥${(price*count).toFixed(2)}
		`);
		console.log(`下单时间:${new Date(orderTime).toLocaleString()}`);
		
		var i=new Date().getDay();
		console.log(`今天星期${arr[i]}`);//这个时间是动态的,会随着日期改变
		</script>
	</body>
</html>

setDate

设置一个月的某一天

通过setDate对天数进行加减

 案列:设置成一个月的某一天

设置成一个月的某一天

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			// 因为时间戳我还不是很懂,就用了两个变量进行对比
			
			// 现在是2022.8.9号
			var time=new Date();
			console.log(time);
			
			// 现在是2022.8.9号,改成2022.8.20号
			var period=new Date();
			period.setDate(20);
			console.log(period);
		</script>
	</body>
</html>

 案列:通过setDate对天数进行加减

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script>
			// 现在是2022.8.9号,加15天
			var time=new Date();
			// 加上15天
			var age=(time.setDate(time.getDate()+15));
			console.log(age,"time");//1661311104941
		    // 把时间戳转换为日期
			var e=new Date(1661311104941);
			console.log(e,"time");	
			
			
			// 现在是2022.8.9号,减10天
			var time2=new Date();
			// 减去10天
			var age2=(time2.setDate(time2.getDate()-10));
			console.log(age2,"time2");
			// 把时间戳进行转换
			var e2=new Date(1659151433175);
			console.log(e2,"time2");
			// 再转换一次格式
			console.log(e2.toLocaleString(),"time2");
		</script>
	</body>
</html>

常见错误

ƒ toLocaleString() { [native code] }

会出现这个错的原因就是toLocalString忘了跟()

定时器属于node.js

其他内容以后菜鸟教程补JavaScript Date 对象 | 菜鸟教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值