- 创建Date对象
- Date对象的属性
- Date对象的方法
1、创建Date对象:
//1.获取当前时间必须实例化
var now = new Date();
console.log(now); //Sun Apr 19 2020 09:59:40 GMT+0800 (中国标准时间)
//2.Date()里面的参数是时间,则返回该时间(参数是字符串型)
var time = new Date("2020-4-19");
var times = new Date("2020/4/19");
console.log(time);
console.log(times); //Sun Apr 19 2020 00:00:00 GMT+0800 (中国标准时间)
//3.加上时分秒
var time1 = new Date("2020-4-19 10:04:23");
console.log(time1); //Sun Apr 19 2020 10:04:23 GMT+0800 (中国标准时间)
//4.Date()里面的参数是数字型,月份会比实际多一个月
var time2 = new Date(2020, 4, 19);
console.log(time2);//Tue May 19 2020 00:00:00 GMT+0800 (中国标准时间)
2、Date对象的属性:
constructor属性、prototype属性
1.constructor属性--可以判断一个对象的类型,该属性引用的是对象的构造函数。(语法格式:object.constructor)
//1.constructor属性--可以判断一个对象的类型,该属性引用的是对象的构造函数。
//语法格式:object.constructor
var newDate = new Date();
if (newDate.constructor == Date) {
document.write("日期型对象");//日期型对象
}
2.prototype属性--为Date对象添加自定义的属性或方法。语法:Date.prototype.name=value(value:添加属性的值或执行方法的函数 )
//2.prototype属性--为Date对象添加自定义的属性或方法。
//语法:Date.prototype.name=value
//value:添加属性的值或执行方法的函数
var newDate = new Date();
Date.prototype.year = newDate.getFullYear();
alert(newDate.year);//2020
3.日期对象的方法:
(1)获取日期和时间:
方法 | 说明 |
getYear() | 从Date对象以两位或4位数字返回年份 |
getFullYear() | 从Date对象以四位数字返回年份 |
getMonth() | 从Date对象返回月份(0~11) |
getDate() | 从Date对象返回一个月中的某一天(1~31) |
getDay() | 从Date对象返回一周中的某一天(0~6) |
getHours() | 返回Date对象的小时(0~23) |
getMinutes() | 返回Date对象的分钟(0~59) |
getSeconds() | 返回Date对象的秒数(0~59) |
getMilliseconds() | 返回Date对象的毫秒(0~999) |
getTime() | 返回1970年1月1日午夜至指定时间的毫秒数 |
//方法案例
var Date = new Date();
console.log(Date.getFullYear()); //2020
console.log(Date.getMonth()); //3,月份是0-11的,所以比实际小1,要加一
console.log(Date.getMonth() + 1);
console.log(Date.getDate()); //19,返回当天号数
console.log(Date.getDay()); //0,返回一周中的某一天(0~6),周日是0,周六是6
console.log(Date.getHours()); //10,返回Date对象的小时(0~23)
//写一个时间为2020年4月19号 星期日:
//写一个时间为2020年4月19号 星期日
var Date = new Date();//必须先声明一个日期对象,才能引用日期对象的方法
var year = Date.getFullYear(); //年
var month = Date.getMonth() + 1; //月
var dates = Date.getDate(); //日
var day = Date.getDay(); //周几
var arr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
//定义一个数组,符合date.getDay()的语法,周几从0开始到6结束,分别是周日到周六。
console.log("今天是:" + year + "年" + month + "月" + dates + "日" + arr[day]);
//今天是:2020年4月19日星期日
(2)设置日期和时间的方法:
方法 | 说明 |
setYear() | 设置 Date 对象中的年份(两位或四位数字) |
setFullYear() | 设置 Date 对象中的年份(四位数字) |
setMonth() | 设置 Date 对象中月份(0~11) |
setDate() | 设置 Date 对象中月的某一天(1~31) |
setHours() | 设置 Date 对象中的小时(0~23) |
setMinutes() | 设置 Date 对象中的分钟(0~59) |
setSeconds() | 设置 Date 对象中的秒钟(0~59) |
setMilliseconds() | 设置 Date 对象中的毫秒(0~999) |
setTime() | 使用毫秒数设置日期和时间 |
(3)转换为字符串的方法:
方法 | 说明 |
toString() | 把Date对象转换为字符串 |
toTimeString() | 把Date对象的时间部分转换为字符串 |
toDateString() | 把Date对象的日期部分转换为字符串 |
toLocaleString() | 根据本地时间格式,把Date对象转换为字符串 |
toLocaleTimeString() | 根据本地时间格式,把Date对象的时间部分转换为字符串 |
toLocaleDateString() | 根据本地时间格式,把Date对象的日期部分转换为字符串 |
4.案例练习:
4.1.让用户输入一个名字列表,并将名字保存在数组中。继续获取下一个名字,直到用户输入为空为止。然后按升序排列名字顺序,并输出到页面上,每个名字各占一行。
分析:
(1)定义变量:输入的名字(inputName)、名字存放的数组(arr)
(2)将用户输入的名字存放在数组(arr[arr.length]=inputName)
(3)直到用户输入为空为止(while(inputName==" ")),可以用若不为空则继续输入名字存放数组中
(4)sort(arr)排序
(5)每个名字各占一行可以用“<br/>”
var inputName = "";//记住该变量的用户输入必须放在while循环中
var namesArray = [];
while ((inputName = prompt("Please enter your name", "")) != "") {
namesArray[namesArray.length] = inputName;
}
namesArray.sort();
var namesList = namesArray.join("<br/>");
document.write(namesList);
4.2.时间戳:获得Date总的毫秒数,不是当前毫秒数,而是距离1970年1月1日过了多少毫秒数
(1)通过valueOf()、getTime()获得Date总的毫秒数
var date = new Date();
console.log(date.getTime());
console.log(date.valueOf());
(2)最简单也是最常用的方法:+new Date()返回的就是总的毫秒数
var date1 = +new Date(); //+new Date()返回的就是总的毫秒数
console.log(date1);
(3)h5新增的 获得总的毫秒数,Date.now()
console.log(Date.now());
4.3.格式化日期输出想要的格式为00:00:00
(1)格式化日期输出时分秒
//格式化日期输出时分秒
var date = new Date(); //15:16:18
console.log(date.getHours()); //15
console.log(date.getMinutes()); //16
console.log(date.getSeconds()); //18
(2)要求封装一个函数返回当前时分秒的格式为00:00:00
//要求封装一个函数返回当前时分秒的格式为00:00:00
function getTime() {
var time = new Date();
var h = time.getHours();
var m = time.getMinutes();
var s = time.getSeconds();
return h + ":" + m + ":" + s;
}
console.log(getTime()); //15:16:18
(3)要求当秒为0时变为00,1为01...不足10前面补零
//要求当秒为0时变为00,1为01...
function getTimes() {
var times = new Date();
var h = times.getHours();
h = h < 10 ? "0" + h : h; //三元表达式不足10前面补零
var m = times.getMinutes();
m = m < 10 ? "0" + m : m; //三元表达式不足10前面补零
var s = times.getSeconds();
s = s < 10 ? "0" + s : s; //三元表达式不足10前面补零
return h + ":" + m + ":" + s;
}
console.log(getTimes());
4.4做一个倒计时的效果,不足十前面补零(比如倒计时01:50:00)
分析:
(1)核心算法:输入的时间减去现在的时间就是剩余的时间,即倒计时。但是不能用时分秒相减,比如00分-25分,结果会是负数的。
(2)用时间戳来做。用户输入时间的总毫秒数减去当前时间的总毫秒数,就是剩余时间的毫秒数。
(3)把剩余时间总毫秒数转换为日,时,分,秒(即时间戳转换为时分秒)
公式如下:
- d=parseInt(总秒数/60/60/24);//计算天数;
- h=parseInt(总秒数/60/60%24);//计算时数;
- m=parseInt(总秒数/60%60);//计算分;
- s=parseInt(总秒数%60);//计算当前秒数;
//做一个倒计时的效果,不足十前面补零(比如倒计时01:50:00)
function countDown(time) {
var nowTime = +new Date(); //返回当前时间总的毫秒数
var inputTime = +new Date(time); //返回用户输入的时间总毫秒数
var times = (inputTime - nowTime) / 1000; //1000毫秒等于1秒,剩余时间总秒数
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("2020-4-20 00:00:00")); //00天07时04分41秒
var date = new Date();
console.log(date);//当前时间2020-4-19 16:55:19