<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<div class="today">获取今天时间</div>
<div class="accuracy_time">获取今天时间准确时间</div>
<div class="weektoday">周几</div>
<div class="yesterday">昨天</div>
<div class="tomorrow">明天</div>
<div class="week_begin_end">本周开始到结束日期</div>
<div class="month_begin_end">本月开始到结束 有多少天</div>
<body>
</body>
<script src="./jquery-1.12.3.min.js"></script>
<script>
$(".today").click(function () { //获取今天时间
let date = new Date(); //当前的时间戳
const year = date.getFullYear()
let month = date.getMonth() + 1
let day = date.getDate()
if (month < 10) {
month = '0' + month
}
if (day < 10) {
day = '0' + day
}
console.log(year + '-' + month + '-' + day, '今天日期');
console.log('----------');
})
$(".accuracy_time").click(function () { //获取今天时间准确时间
let date = new Date(); //当前的时间戳
const year = date.getFullYear()
let month = date.getMonth() + 1
let day = date.getDate()
let hour = date.getHours()
let minute = date.getMinutes()
let second = date.getSeconds()
if (month < 10) {
month = '0' + month
}
if (day < 10) {
day = '0' + day
}
hour = hour.toString().padStart(2, '0')
minute = minute.toString().padStart(2, '0')
second = second.toString().padStart(2, '0')
console.log(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second, '当前具体时间');
console.log('----------');
})
$(".weektoday").click(function () { //周几
let date = new Date(); //当前的时间戳
const thisDay = new Date().getDay()
const weekDay = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
console.log(weekDay[thisDay] + ' 周几');
console.log('----------');
})
$(".yesterday").click(function () { //昨天
let date = new Date(); //当前的时间戳
date.setDate(date.getDate() - 1); //设置天数 -1 天,昨天的日期
let year = date.getFullYear();//年
let month = date.getMonth() + 1;//月
let day = date.getDate();//日
if (month < 10) {
month = '0' + month
}
if (day < 10) {
day = '0' + day
}
console.log(year + '-' + month + '-' + day, '昨天');
console.log('----------');
})
$(".tomorrow").click(function () { //昨天
let date = new Date(); //当前的时间戳
date.setDate(date.getDate() + 1); //设置天数 +1 天,明天的日期
let year = date.getFullYear();//年
let month = date.getMonth() + 1;//月
let day = date.getDate();//日
if (month < 10) {
month = '0' + month
}
if (day < 10) {
day = '0' + day
}
console.log(year + '-' + month + '-' + day, '明天');
console.log('----------');
})
$(".week_begin_end").click(function () { //本周开始到结束日期
let weekList = [];
let date = new Date();//当前的时间戳
if (date.getDay() == "0") {
date.setDate(date.getDate() - 6);
} else {
date.setDate(date.getDate() - date.getDay() + 1);
}
let myDate = date.getDate();
let myMonth = date.getMonth() + 1;
if (date.getDate() < 10) {
myDate = '0' + myDate;
}
if (date.getMonth() + 1 < 10) {
myMonth = '0' + myMonth;
}
weekList.push(date.getFullYear() + "-" + myMonth + "-" + myDate);
for (var i = 0; i < 6; i++) {
date.setDate(date.getDate() + 1);
myDate = date.getDate();
myMonth = date.getMonth() + 1;
if (date.getDate() < 10) {
myDate = '0' + myDate;
}
if (date.getMonth() + 1 < 10) {
myMonth = '0' + myMonth;
}
weekList.push(date.getFullYear() + "-" + myMonth + "-" + myDate);
}
console.log(weekList, '本周开始到结束日期');
console.log('----------');
})
$(".month_begin_end").click(function () { //月初 月末 有多少天
var nowDate = new Date();
var cloneNowDate = new Date();
var fullYear = nowDate.getFullYear();
var month = nowDate.getMonth() + 1; // getMonth 方法返回 0-11,代表1-12月
var endOfMonth = new Date(fullYear, month, 0).getDate(); // 获取本月最后一天
function getFullDate(targetDate) {
var D, y, m, d;
if (targetDate) {
D = new Date(targetDate);
y = D.getFullYear();
m = D.getMonth() + 1;
d = D.getDate();
} else {
y = fullYear;
m = month;
d = date;
}
m = m > 9 ? m : '0' + m;
d = d > 9 ? d : '0' + d;
return y + '-' + m + '-' + d;
};
let endDate = getFullDate(cloneNowDate.setDate(endOfMonth));//当月最后一天
let starDate = getFullDate(cloneNowDate.setDate(1));//当月第一天
console.log(starDate, endDate, iDays(starDate, endDate), '月初 月末 有多少天'); //月初 月末 当前月共有多少天
console.log('----------');
})
function iDays(sDate1, sDate2) {
var aDate, oDate1, oDate2, iDays;
aDate = sDate1.split("-");
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
aDate = sDate2.split("-");
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
return iDays + 1; //返回相差天数
}
</script>
</html>