JavaScript Date(日期) 对象
今天我们重点学习JavaScript中的Date对象。
Date对象用于处理日期和时间。可以通过new关键词来定义Date对象。
获取时间
var now = new Date();
console.log(now);
var date1 = new Date(new Date(2022,0,1,09,30,00));
console.log(date1);
var date2 = new Date(2022,0,9);
console.log(date2);
var date3 = new Date(1652076712317);
console.log(date3);
- 当我们获取到时间之后, 可以通过其子函数获取对应的年、月、日、时、分、秒、时间戳等。
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var time = now.getTime();
- 有了这些子函数,我们就可以利用setDate等函数对时间进行动态设置,如标题中提到的获取昨天的时间。
var now = new Date();
var yestoday = now.setDate(now.getDate()-1);
console.log(yestoday)
yestoday = new Date(yestoday);
console.log(yestoday)
var now = new Date();
var yestoday = new Date(now.setDate(now.getDate()-1));
console.log(yestoday)
- 如今天为9号,需获取上个月9号的时间,则需使用setMonth,其他如setFullYear,setHours均同理。
var now = new Date();
var yesMonth = now.setMonth(now.getMonth()-1);
console.log(yesMonth)
yesMonth = new Date(yesMonth);
console.log(yesMonth)
var now = new Date();
var yesMonth = new Date(now.setMonth(now.getMonth()-1));
console.log(yesMonth);
格式化时间
当我们能够正确拿到时间之后,需要对时间进行格式化,以便更好的在界面上展示,或者使用正确的格式与后台数据进行交互。
var now = new Date();
console.log(now);
var formatNow = formatDate(now);
console.log(formatNow);
function formatDate(time) {
if (time != null) {
var datetime = new Date();
datetime.setTime(time);
var year = datetime.getFullYear();
var month = (datetime.getMonth() + 1) < 10 ? "0" + (datetime.getMonth() + 1) : (datetime.getMonth() + 1);
var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
return year + "-" + month + "-" + date + ' ' + hour + ':' + minute + ':' + second;
} else {
return "---";
}
}
结合上一节的内容,整理获取昨天、上个月格式化的时间。
var now = new Date();
var yestoday = formatDate(new Date(now.setDate(now.getDate()-1)));
console.log(yestoday)
var now = new Date();
var yesMonth = formatDate(new Date(now.setMonth(now.getMonth()-1)));
console.log(yesMonth);
function formatDate(time) {
if (time != null) {
var datetime = new Date();
datetime.setTime(time);
var year = datetime.getFullYear();
var month = (datetime.getMonth() + 1) < 10 ? "0" + (datetime.getMonth() + 1) : (datetime.getMonth() + 1);
var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
return year + "-" + month + "-" + date + ' ' + hour + ':' + minute + ':' + second;
} else {
return "---";
}
}
实际业务
由于列表数据量过大,默认进入页面时只展示一周以内的数据。需要在进入页面时获取当前系统时间前7天的时间作为开始时间传入后台。后台接收的格式为 yyyy-mm-dd。
var now = new Date();
var beforeDay = formatDate(new Date(now.setDate(now.getDate()-7)));
console.log(beforeDay)
function formatDate(time) {
if (time != null) {
var datetime = new Date();
datetime.setTime(time);
var year = datetime.getFullYear();
var month = (datetime.getMonth() + 1) < 10 ? "0" + (datetime.getMonth() + 1) : (datetime.getMonth() + 1);
var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
return year + "-" + month + "-" + date;
} else {
return "---";
}
}
|