JavaScript基础知识——Date对象

			/* 
			Date对象
				在JS中使用一个Date对象来表示一个时间
			 */

			/* 创建一个Date对象
			如果直接使用构造函数创建一个Date对象,则会封装为当前代码执行的时间 */

			var a = new Date();
			console.log(a);

			/* 
			创建一个指定的时间对象
			需要在构造函数中传递一个表达时间的字符串作为参数
			 日期的格式 月份/日/年 时:分:秒
			 */

			var a1 = new Date("12/10/2021 12:10:45")
			console.log(a1);

			/* 
			getFullYear()
			获取当前时间对象的年份
			 */
			
			var year = a.getFullYear();
			console.log("year=" + year);

			/* 
			get.getMonth()
			获取当前时间对象的月份
			0表示一月份
			1表示二月份
			...
			11表示十二月份
			 */

			var month = a.getMonth();
			console.log("month=" + (month + 1));
			
			/* 
			getDate()
				获取当前时间对象是几日
			 */

			var date = a.getDate();
			console.log("date=" + date);
			
			/* 
			getDay()
			获取当前时间对象是周几
			会返回一个0-6的值
			0表示周日
			1表示周天
			 */
			
			var day = a.getDay();
			console.log("day=" + day);

			
			/* 
			getHours()
				获取当前时间对象的小时
			 */
			
			var hours = a.getHours();
			console.log("hours=" + hours);
			
			/* 
			getMinutes()
				获取当前时间对象的分钟数
			 */
			
			var minutes = a.getMinutes();
			console.log("minutes=" + minutes);
			
			/* 
			getSeconds()
				获取当前时间对象的秒数
			 */
			
			var seconds = a.getSeconds();
			console.log("seconds=" + seconds);
			
			/* 
			getMilliseconds()
				获取当前时间对象的毫秒数
			 */
			
			var milliseconds = a.getMilliseconds();
			console.log("milliseconds=" + milliseconds);
			
			/* 
			getTime()
			获取当前时间对象的时间戳
			时间戳,指的是从格林威治标准时间的1970年1月1日,0时0分0秒
				到当前日期所花费的毫秒数(1秒 = 1000毫秒)
			计算机在底层保存的时间时使用都是时间戳
			 */
			
			var time = a.getTime();
			console.log("time=" + time);
			console.log(time / 1000 / 60 / 60 / 24 / 365);

			// 获取一个循环所用的毫秒数
			var start = Date.now();
			console.log(start);
			for (var i = 0; i < 100; i++) {
				console.log(i);
			}
			var end = Date.now();
			console.log("执行了:" + (end - start) + "毫秒");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值