1.日期对象
- Date对象
- 创建日期对象的方法:
- new关键字。 var date = new Date();
- new关键字,但是可以参入参数。
- 不参入参数默认是当前的时间
【注意】
- var date3 = new Date(2020,06,01,15,30,06);
- 使用上面的方式,月份是从0开始计算的,06就是七月份。
【注意】
- 我们现在在东八区,所以距离格林威治也就是系统真正的时间有8个小时的时差。
例
// 1.new 关键字创建对象
var date = new Date();
console.log(date); //当前系统时间 例:Tue Jun 01 2021 20:08:26 GMT+0800 (中国标准时间)
// 2、传入参数 年/月/日
var date2 = new Date("2020/06/01 15:30:06");
console.log(date2); //Mon Jun 01 2020 15:30:06 GMT+0800 (中国标准时间)
// 3.传入参数的格式
var date3 = new Date(2020, 06, 01, 15, 30, 06);
console.log(date3); //Wed Jul 01 2020 15:30:06 GMT+0800 (中国标准时间)
2.日期对象的方法
Date.parse(date)
- 参数:日期对象
- 功能:将日期对象转为毫秒数。
setTime(n)
- 参数:数字,表示毫秒数
- 功能: 设置距离1970年1月1日0零整的毫秒数时间。 比如:设置1000ms,就表示系统的开始时间(1970年1月1日 00:00:00 )的1000ms后的时间。
getTime()
- 功能:获取1970年1月1日0零整到日期对象之间的毫秒数。
setDate()
- 功能:设置日期对象的日期天数。
getDate()
- 功能: 获取日期对象对应的日期天数。
getDay()
- 功能: 获取日期对象中在一周内的第几天 (0-6) 星期日为0
setMonth()
- 功能:设置日期对象的月份。
【注意】月份是从0开始的,范围是0-11 1月份是0,2月份是1.
getMonth()
- 功能:获取日期对象的月份。
setFullYear()
- 功能:设置日期对象的年份
getFullYear()
- 功能:获取日期对象的年份
setHours()
- 功能:设置日期对象的小时数。
getHours
- 功能:获取日期对象的小时数。
setMinutes()
- 功能:设置日期对象的分钟数
getMinutes()
- 功能:获取日期对象的分钟数
setSeconds()
- 功能:设置日期对象的秒数
getSeconds()
- 功能:获取日期对象的秒数
setMilliseconds
- 功能:设置日期对象的毫秒数
getMilliseconds
- 功能:获取日期对象的毫秒数
getTimezoneOffset()
- 功能:获取本地时间与格林威治时间之间的分钟差。
【注意】东八区 返回 -480
Date.parse()与getTime()区别
- 共同点:距离1970年1月1日零点整的毫秒数。
- Date.parse() 获取的是 年月日时分秒转换过来的毫秒数,不包含秒数后面的毫秒数。
- getTime()获取的包含毫秒数
例
var date = new Date();
console.log(Date.parse(date)); //1970年1月1日0零整到当前时间的毫秒数(精确到秒)
console.log(date.getTime()); //1970年1月1日0零整到当前时间的毫秒数(精确到毫秒)
//----------------------------------------------------------------------------------------------------
var date = new Date();
var num = Date.parse(date);
console.log(num); //1970年1月1日0零整到当前时间的毫秒数(精确到秒)
//----------------------------------------------------------------------------------------------------
var date = new Date();
date.setTime(1000);
console.log(date); //Thu Jan 01 1970 08:00:01 GMT+0800 (中国标准时间)
//----------------------------------------------------------------------------------------------------
var date = new Date("1970/01/01 09:00:00")
console.log(date); //Thu Jan 01 1970 09:00:00 GMT+0800 (中国标准时间)
console.log(date.getTime()); //3600000
//----------------------------------------------------------------------------------------------------
var date = new Date();
console.log(date.getDate()); //1
date.setDate(23);
console.log(date); //Wed Jun 23 2021 20:33:52 GMT+0800 (中国标准时间)
//----------------------------------------------------------------------------------------------------
var date = new Date();
date.setDate(25);
console.log(date.getDay()); //5
//----------------------------------------------------------------------------------------------------
var date = new Date();
date.setMonth(1);
console.log(date); //Mon Feb 01 2021 20:35:56 GMT+0800 (中国标准时间)
//----------------------------------------------------------------------------------------------------
var date = new Date();
console.log(date.getMonth()); //5
//----------------------------------------------------------------------------------------------------
var date = new Date();
date.setFullYear(2022);
console.log(date.getFullYear()); //2022
//----------------------------------------------------------------------------------------------------
var date = new Date();
date.setHours(12);
console.log(date.getHours()); //12
//----------------------------------------------------------------------------------------------------
var date = new Date();
date.setMinutes(18);
console.log("0"+date.getMinutes()); //018
//----------------------------------------------------------------------------------------------------
var date = new Date();
date.setSeconds(50)
console.log(date.getSeconds()); //50
//----------------------------------------------------------------------------------------------------
var date = new Date();
console.log(date.getMilliseconds()); //当前时间的毫秒数
//----------------------------------------------------------------------------------------------------
var date = new Date();
console.log(date.getTimezoneOffset()); //-480
3.日期格式化
日期格式化的方法:
toDateString()
- 输出 星期几 月,日,年。
toTimeString()
- 输出 时分秒 时区。
toLocaleDateString()
- 输出 yyyy-mm-dd。
toLocaleTimeString()
- 输出时分秒以及上午/下午(AM,PM)
toUTCString()
- 输出格林威治时间 星期几 日 月 年 时 分 秒 时区。
例
var date = new Date();
console.log(date.toDateString()); //Tue Jun 01 2021 输出 星期几 月,日,年。
//--------------------------------------------------------------------------------------------
var date = new Date();
console.log(date.toTimeString()); //20:45:35 GMT+0800 (中国标准时间) 输出 时分秒 时区。
//--------------------------------------------------------------------------------------------
var date = new Date();
console.log(date.toLocaleDateString()); //2021/6/1 输出 yyyy-mm-dd。
//--------------------------------------------------------------------------------------------
var date = new Date();
console.log(date.toLocaleTimeString()); //下午8:48:09 输出时分秒以及上午/下午(AM,PM)
//--------------------------------------------------------------------------------------------
var date = new Date();
console.log(date.toUTCString()); //Tue, 01 Jun 2021 12:44:33 GMT 输出格林威治时间 星期几 日 月 年 时 分 秒 时区。
4.自定义函数
- 按中国人的方式输出日期(年、月、日、星期、时间)
例
// 自定义日期格式化方法
// 2021年6月1日 星期二 20:50:49
function showTime(date) {
// 2、获取年份
var year = date.getFullYear();
//3. 获取月份
var mon = date.getMonth() + 1;
//4.获取日份
var day = date.getDate();
//5.获取小时数
var h = date.getHours();
//6.获取分钟数
var min = date.getMinutes();
//7.获取秒数
var sec = date.getSeconds();
//8.获取星期几
var week = date.getDay();
week = num2Chinese(week);
var str = year + "年" + mon + "月" + day + "日 星期" + week + " " + addZero(h) + ":" + addZero(min) + ":" + addZero(sec);
return str;
}
// 专门用来将数字转大写中文的。
function num2Chinese(num) {
// 程序中,星期日返回的是0,所以必须将日放在最前面。
var arr = ['日', '一', '二', '三', '四', '五', '六'];
return arr[num];
}
// 补0
function addZero(num) {
if (num < 10) {
return "0" + num;
} else {
return num;
}
}
console.log(showTime(new Date())); //2021年6月1日 星期二 20:50:49
案例练习
- 定义一个函数,传入一个数字n,输出n天后的日期。
- 步骤:
- 获取当前的时间。
- 获取当前时间的天数
- 天数+n
- 将+n后的天数设置给日期对象。
function afterDay(n) {
var date = new Date();
date.setDate(date.getDate() + n);
return date;
}
console.log(showTime(afterDay(20))); //2021年6月21日 星期一 23:11:51 20天后的现在时间
- 将当前时间输出到页面上(yyyy-MM-DD hh:mm:ss)
// 1.创建一个日期对象。
var date = new Date();
// 2、获取年份
var year = date.getFullYear();
//3. 获取月份
var mon = date.getMonth() + 1;
//4.获取日份
var day = date.getDate();
//5.获取小时数
var h = date.getHours();
//6.获取分钟数
var min = date.getMinutes();
//7.获取秒数
var sec = date.getSeconds();
document.write(year + "-" + (mon < 10 ? "0" + mon : mon) + "-" + (day < 10 ? "0" + day : day) + " " + (h < 10 ? "0" + h : h) + ":" + (min < 10 ? "0" + min : min) + ":" + (sec < 10 ? "0" + sec : sec)); //2021-06-01 23:16:27 系统当前时间