效果图:
代码:
<!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>
<style>
#year {
width: 6rem;
font-size: 1rem;
margin-bottom: 1rem;
}
button {
width: 3rem;
height: 3rem;
margin-bottom: 0.5rem;
background-color: white;
}
button:hover {
cursor: pointer;
}
.text {
display: flex;
}
#content {
margin-left: 1rem;
}
table {
user-select: none;
}
p {
height: 1rem;
}
</style>
</head>
<body>
<div class="text">
<div class="date">
<select name="" id="year" οnchange="changeSelect(value)"></select>
<div>
<button id="Jan" οnclick="change('Jan')">Jan</button>
<button id="Feb" οnclick="change('Feb')">Feb</button>
</div>
<div>
<button id="Mar" οnclick="change('Mar')">Mar</button>
<button id="Apr" οnclick="change('Apr')">Apr</button>
</div>
<div>
<button id="May" οnclick="change('May')">May</button>
<button id="Jun" οnclick="change('Jun')">Jun</button>
</div>
<div>
<button id="Jul" οnclick="change('Jul')">Jul</button>
<button id="Aug" οnclick="change('Aug')">Aug</button>
</div>
<div>
<button id="Sept" οnclick="change('Sept')">Sept</button>
<button id="Oct" οnclick="change('Oct')">Oct</button>
</div>
<div>
<button id="Nov" οnclick="change('Nov')">Nov</button>
<button id="Dec" οnclick="change('Dec')">Dec</button>
</div>
</div>
<div id="content"></div>
</div>
</body>
<script>
var tody = new Date();
var year = tody.getFullYear();
var month = tody.getMonth();
var day = tody.getDate();
var t = month;
var isLeap = year % 400 == 0 ? 1 : ((year % 100 != 0 && year % 4 == 0) ? 1 : 0);
var aMouth = [31, 28 + isLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];//创建月份尾数表,作为每张日历最后一个数
var aWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var oDetail = document.getElementById('content');//显示当前日期的栏目
function oninit() {
let arr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
let array = [];
document.getElementById(arr[month]).style.backgroundColor = 'red';
document.getElementById(arr[month]).style.color = 'white';
// 年份选择显示当前年份前后60年
for (let i = 60; i > -1; i--) {
array.push(year - i);
}
for (let i = 1; i < 61; i++) {
array.push(year + i);
}
for (let i = 0; i < array.length; i++) {
document.getElementById('year').options.add(new Option(array[i], array[i]));
if (array[i] === year) {
document.getElementById('year').options.selectedIndex = i;
}
}
time(tody);
}
oninit();
// 下拉框年份变化调用
function changeSelect(k) {
oDetail.childNodes[0].remove();
time(new Date(k, t, day));
}
function time(k) {
var cTable = document.createElement('table'); //创建table
var cTHead = document.createElement('thead'); //创建thead
var cTBody = document.createElement('tbody'); //创建tbody
year = k.getFullYear();
month = k.getMonth();
day = k.getDate();
isLeap = year % 400 == 0 ? 1 : ((year % 100 != 0 && year % 4 == 0) ? 1 : 0);
aMouth = [31, 28 + isLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
t = month;
// 创造一个table表格头
var cTr0 = document.createElement('tr');
for (var k = 0; k < 7; k++) {
var cTd0 = document.createElement('td');
cTd0.innerHTML = aWeek[k];
cTd0.style.borderBottom = '1px solid #bbb';
cTd0.style.paddingBottom = '0.5rem';
cTr0.appendChild(cTd0);
}
cTHead.appendChild(cTr0);
cTable.appendChild(cTHead);
var firstDate = new Date(year, month, 1); //获取当月第一天
var dayOfWeek = firstDate.getDay();//获取当月的第一天是星期几
for (var i = 0; i < 6; i++) {
var ctr = document.createElement('tr');
for (var j = 0; j < 7; j++) {
var re = i * 7 + j;
var re1 = re - dayOfWeek + 1;
var ctd = document.createElement('td');
if (re1 <= 0) {
if (dayOfWeek > 0) {
// 获取上个月最后一天
let sf = new Date(document.getElementById('year').value, t, 0).getDate();
let ti = aWeek[new Date(document.getElementById('year').value, t - 1, sf + re1).getDay()];
ctd.style.border = '1px solid #bbb';
ctd.style.width = '8rem';
ctd.style.opacity = 0.2;
if (ti === 'Sun' || ti === 'Sat') {
ctd.style.backgroundColor = '#1E90FF';
ctd.innerHTML = '<div>' + '<p style="font-size: 0.875rem;">' + (sf + re1) + '</p>' + '<p style="font-size: 0.4rem;">' + dateTimes(new Date(document.getElementById('year').value, t - 1, sf + re1)) + '</p>' + '<p style="color:red; text-align: right; font-size: 0.4rem;">休息日</p></div>';
} else {
ctd.innerHTML = '<div>' + '<p style="font-size: 0.875rem;">' + (sf + re1) + '</p>' + '<p style="font-size: 0.4rem;">' + dateTimes(new Date(document.getElementById('year').value, t - 1, sf + re1)) + '</p>' + '<p style="color:black; text-align: right; font-size: 0.4rem;">工作日</p></div>';
}
}
} else if (re1 > aMouth[month]) {
let sf = new Date(document.getElementById('year').value, t + 1, 0).getDate();
let ti = aWeek[new Date(document.getElementById('year').value, t + 1, re1 - sf).getDay()];
ctd.style.border = '1px solid #bbb';
ctd.style.width = '8rem';
ctd.style.opacity = 0.2;
if (ti === 'Sun' || ti === 'Sat') {
ctd.style.backgroundColor = '#1E90FF';
ctd.innerHTML = '<div>' + '<p style="font-size: 0.875rem;">' + (re1 - sf) + '</p>' + '<p style="font-size: 0.4rem;">' + dateTimes(new Date(document.getElementById('year').value, t + 1, re1 - sf)) + '</p>' + '<p style="color:red; text-align: right; font-size: 0.4rem;">休息日</p></div>';
} else {
ctd.innerHTML = '<div>' + '<p style="font-size: 0.875rem;">' + (re1 - sf) + '</p>' + '<p style="font-size: 0.4rem;">' + dateTimes(new Date(document.getElementById('year').value, t + 1, re1 - sf)) + '</p>' + '<p style="color:black; text-align: right; font-size: 0.4rem;">工作日</p></div>';
}
} else {
let ti = aWeek[new Date(document.getElementById('year').value, t, re1).getDay()];
ctd.style.border = '1px solid #bbb';
ctd.style.width = '8rem';
if (re1 == day && month == tody.getMonth() && year == tody.getFullYear()) {
ctd.style.color = 'red';
}
if (ti === 'Sun' || ti === 'Sat') {
ctd.style.backgroundColor = '#1E90FF';
ctd.innerHTML = '<div>' + '<p style="font-size: 0.875rem;">' + re1 + '</p>' + '<p style="font-size: 0.4rem;">' + dateTimes(new Date(document.getElementById('year').value, t, re1)) + '</p>' + '<p style="color:red; text-align: right; font-size: 0.4rem;">休息日</p></div>';
} else {
ctd.innerHTML = '<div>' + '<p style="font-size: 0.875rem;">' + re1 + '</p>' + '<p style="font-size: 0.4rem;">' + dateTimes(new Date(document.getElementById('year').value, t, re1)) + '</p>' + '<p style="color:black; text-align: right; font-size: 0.4rem;">工作日</p></div>';
}
}
ctr.appendChild(ctd); //将创造出来的td插入到tr中
}
cTBody.appendChild(ctr); //将创造出来的tr插入到tbody中
}
cTable.appendChild(cTBody); //将创造出来的tbody插入到table中
oDetail.appendChild(cTable);
}
// 点击月份背景和字体颜色变化
function change(k) {
let arr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
for (let i = 0; i < arr.length; i++) {
if (k === arr[i]) {
document.getElementById(arr[i]).style.backgroundColor = 'red';
document.getElementById(arr[i]).style.color = 'white';
oDetail.childNodes[0].remove();
time(new Date(document.getElementById('year').value, i, day));
t = i;
} else {
document.getElementById(arr[i]).style.backgroundColor = 'white';
document.getElementById(arr[i]).style.color = 'black';
}
}
}
// 获取农历
function dateTimes(time) {
let date = time;
dateString = date.toLocaleString('zh-CN-u-ca-chinese');
dateString = dateString.replace(/(\d+)\s*?年/, (x, y) => {
let result = '';
result = "甲乙丙丁戊己庚辛壬癸".charAt((y - 4) % 10); // 天干
result += "子丑寅卯辰巳午未申酉戌亥".charAt((y - 4) % 12); // 地支
return result;
});
dateString = dateString.split(' ')[0]; // 取年月日
let g = dateString.substr(0, 2);
let m = dateString.substring(2, dateString.match('月').index) + '月';
let d = dateString.match(/\d+/)[0];
if (d < 11) {
d = '0' + d;
}
// d = d < 11 ? '初' + tool.numberToString(d) : tool.numberToString(d);
let animals = ["猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"];
let index = date.toLocaleString('zh-CN-u-ca-chinese').substr(0, 4) % 12;
let a = animals[index] + '年';
let arr = ['初', '十', '廿', '三十'];
let str = ['一', '二', '三', '四', '五', '六', '七', '八', '九'];
if (parseInt(d.split('')[1]) != 0) {
d = arr[parseInt(d.split('')[0])] + str[parseInt(d.split('')[1] - 1)];
} else {
d = arr[parseInt(d.split('')[0])];
}
return g + a + m + d;
}
// 参考:百度查询农历日期推导原理,以及部分原生js的api。
</script>
</html>