ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] # 4.1 Date概述 ●Date 对象和Math对象不一样,他是一个构造函数 ,所以我们需要实例化后才能使用 ●Date 实例用来处理日期和时间 ![](https://img.kancloud.cn/c7/1a/c71a67ad90bbf38e5ef6d32979456031_1124x394.png) ## 4.2 Date()方法的使用 ### 1.获取当前时间必须实例化 ~~~ var now = new Date() ; console.log (now) ; ~~~ ## 2\. Date()构造函数的参数 如果括号里面有时间,就返回参数里面的时间。 例如日期格式字符串为'2019-5-1' , 可以写成new Date('2019-5-1')或者new Date(2019/5/1') ## 4.3日期格式化 我们想要2019-8-8 8:8:8格式的日期,要怎么办? 需要获取日期指定的部分,所以我们要手动的得到这种格式。 | 方法名 | 说明 | 代码 | | --- | --- | --- | | getFullYear() | 获取**当年** | dObj.getFullYear() | | getMonth()**+1** | 获取**当月**(0-11) | dObj.getMonth() | | getDate() | 获取**当天日期** | dObj.getDate() | | getDay() | 获取**星期几**(周日0 到周六6) | dObj.getDay() | | getHours() | 获取**当前小时** | dObj.getHours() | | getMinutes() | 获取**当前分钟** | dObj.getMinutes() | | getSeconds() | 获取**当前秒钟** | dObj.getSeconds() | ### 格式化日期年月日 var date = new Date(); //必写 console.log(date.getFullYear()); // 返回当前日期的年2019 console.log(date.getMonth() + 1); //月份返回的月份小1个月记得月份+1 呦 console.log(date.getDate()); //返回的是几号 console.log(date.getDay()); // 3周一返回的是 1周六返回的是6但是周日返回的是0 console.log(date.getHours()); //时 console.log(date.getMinutes()); //分 console.log(date.getSeconds()); //秒 ### 获取当前年月日(获取当前日期) (重点) ~~~ var date = new Date(); //必写,不写不行 //我们写一个2019年5月1日星期三 var year = date.getFullYear(); //获取当前年份 var month = date.getMonth() + 1; //获取当前月份(注意 记得+1 ) var dates = date.getDate(); //获取当日 // 因为外国的一周是从0 天开始 所以要用数组来更加好看 var arr = ['星期日’,'星期一','星期二','星期三','星期四’,'星期五','星期六']; var day = date.getDay(); console.log('今天是:+ year +'年' + month'月'+dates+'日arr[day]); ~~~ ### 格式化日期时分秒 var date = newrDatte();te console.log(date .JgetHours()); //时 console.log(date. getMinutes()); //分 console.log(date. getSeconds()); //秒 ### 要求封装一个函数返回当前的时分秒格式08:08:08 (封装函数 时分秒) ~~~ //函数封装 //function(函数声明) getTime(函数名) function getTime() { var time = new Date (); var h = time.getHours(); h=h<10?'0+h:h; // 判断语句的 3元运算符 把右边的值赋予给左边 var m = time . getMinutes(); m = m<10?' 0'+ m : m; var s = time . getSeconds(); s =S<10? '0'+S : s; return h +' :'+m+':'+ s; //返回值(返回这个值 最后输出) } console.log(getTime()); //输出函数名使用 ~~~ ## 4.4获取日期的总的毫秒形式 (离初始时间过了多长时间) Date对象是基于1970年1月1日(世界标准时间)起的毫秒数 为什么计算机起始时间从1970年开始? 我们经常利用总的毫秒数来计算时间,因为它更精确 ### 获得Date.总的毫秒数不 是当前时间的毫秒数而是距离1970年1月1号过了多少毫秒数 ### // 1.通过value0f() getTime() ~~~ var date = new Date( ); console. log(date .valueOf()); //就是我们现在时间距离1970.1.1总的毫秒数 console.1og(date. getTime( )); ~~~ ### // 2.(获取毫秒)简单的写法(最常用的写法) ~~~ var date1 = +new Date(); // +new Date() 返回的就是总的毫秒数 console.log(date1); ~~~ ### // 3. H5新增的获得总的毫秒数 (如果没考虑不兼容可以用) ~~~ console.log(Date.now( )); ~~~ # 实验 ## 案例:倒计时效果 (秒杀倒计时) 做一个倒计时效果 ![](https://img.kancloud.cn/4f/7b/4f7b8d861467b08b72200c72144d6ff1_396x423.png) 案例分析 ![](https://img.kancloud.cn/7e/25/7e2599f221d71ac1b1ed3b5c973d73f5_913x208.png) ![](https://img.kancloud.cn/76/34/763447c89e70de7ca1d79decb99875da_859x180.png) 毫秒转换公式 ~~~ ●d = parseInt(总秒数/ 60/60/24); // 计算天数 ●h= parselnt(总秒数/ 60/60%24) // 计算小时 ●m = parselnt(总秒数/60 %60); // 计算分数 ●s = parselnt(总秒数%60);//计算当前秒数 ~~~ 实验代码 ~~~ //1、函数声明 (参数) //2、返回豪秒 赋值给左边变量名 //3、返回毫秒(里面添加参数 是返回(未来)时间 (自己输入的时间) ) //4、(总秒数)毫秒转换公式 (转换为秒) //5、嵌套上面转化公式 (天,时,分,秒)(总秒数套入 第四的变量名) function countDown(time) { var nowTime = +new Date(); //返回的是当前时间总的毫秒数 var inputTime = +new Date (time); // 返回的是用户输入时间总的毫秒数 var times = (inputTime - nowTime) / 1000; // times是剩余时间总的秒数 var d = parseInt(times / 60 / 60 / 24); //天 d=d<10?'0’+d:d; var h = parseInt(times / 60 / 60 % 24); //时 h=h<10?'0’+h:h; var m = parseInt(times / 60 % 60); //分 m = m< 10?'0'+ m:m; var s = parseInt(times % 60); //当前的秒 s = s< 10?'0+S:s; // 返回值 (返回参数给函数) return d+'天'+h+'时’+ m +'分’+ s + '秒'; } // 输入倒计时时间(未来时间 用字符串类型 时间) console.log( countDown( ' 2019-5-118:00:00')); var date = new Date( ) ; console.log(date); ~~~