虽然搞了很久,出来后成就感杠杠的~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>日历</title>
<style>
body{
font-size: 12px;
}
.main{
width: 520px;
margin: 0 auto;
margin-top: 50px;
border: 1px solid #CCC;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
color: #333;
padding: 5px 0;
box-sizing: border-box;
border-radius: 5px;
}
a{
text-decoration: none;
color: #333;
}
a:hover{
text-decoration: underline;
}
div{
text-align: center;
}
.main div.date-box{
width: 100%;
}
.main .yeah-box{
display: flex;
justify-content: space-between;
align-items: center;
}
.main .yeah-box div{
height: 40px;
line-height: 40px;
}
.main .yeah-box div.yeah{
flex-grow: 1;
font-size: 14px;
}
.main .yeah-box div:not(.yeah){
padding: 0 10px;
}
.main .cur-time{
font-size: 16px;
height: 40px;
line-height: 40px;
font-weight: bold;
}
.main .date-line{
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
list-style: none;
padding: 0;
margin: 0;
width: 97.5%;
padding-left: 2.5%;
}
.main .week-line{
margin-bottom: 10px;
}
.main .date-line li{
list-style: none;
padding: 0;
width: 70px;
text-align: center;
height: 35px;
line-height: 35px;
}
.main .date-line:not(.week-line) li{
cursor: pointer;
margin-bottom: 5px;
}
.main .date-line:not(.week-line) li:not(.disable):hover{
background-color: #666;
color: #FFF;
border-radius: 5px;
}
.main .date-line:not(.week-line) li.active{
background-color: #666;
color: #FFF;
border-radius: 5px;
}
.main .date-line li.disable{
cursor: default;
color: #DDD;
}
.cur-time-btn{
display: block;
background-color: #666;
width: 20%;
margin: 0 auto;
padding: 7px 0;
color: #FFF;
border-radius:5px;
margin-bottom: 10px;
}
</style>
<script>
function getFmtNun(num) {
num = num.toString();
return num.length == 1 ? '0' + num : num;
}
function getWeekStr(week) {
var strs = ['日', '一', '二', '三', '四', '五', '六'];
return strs[week];
}
function getDate(date = '') {
if(date == '') {
var myDate = new Date();
} else {
var myDate = new Date(date);
}
this.year = myDate.getFullYear();
this.month = myDate.getMonth() + 1;
this.monthStr = getFmtNun(this.month);
this.day = myDate.getDate();
this.dayStr = getFmtNun(this.day);
this.week = myDate.getDay();
this.weekStr = getWeekStr(this.week);
this.hour = myDate.getHours();
this.hourStr = getFmtNun(this.hour);
this.minute = myDate.getMinutes();
this.minuteStr = getFmtNun(this.minute);
this.second = myDate.getSeconds();
this.secondStr = getFmtNun(this.second);
return this;
}
var actualCmd;
function dateRender(date = '') {
window.clearTimeout(actualCmd);
this.o = new getDate(date);
this.render = function() {
var curYear = this.o.year;
var curMonth = this.o.month;
var curDay = this.o.day;
var curDate = curYear + '-' + curMonth + '-' + curDay;
document.getElementById('yeah').innerHTML = curYear + '年';
document.getElementById('date').innerHTML = this.o.monthStr + '月' + this.o.dayStr + '日 周' + this.o.weekStr;
document.getElementById('time').innerHTML = this.o.hourStr + ':' + this.o.minuteStr + ':' + this.o.secondStr;
//单天选择
var fullDates = this.getFullDays();
var html = '';
for(var key in fullDates) {
var click = fullDates[key].type == 'prev' ? '' : 'getDayActualDate(\'' + fullDates[key].date + '\');';
var tab = '<li onclick="' + click + '" class="' + (fullDates[key].date == curDate ? 'active' : '') + ' ' + (fullDates[key].type == 'prev' ? 'disable' : '') + '" data-date="' + fullDates[key].date + '">' + fullDates[key].day + '</li>';
html += tab;
}
document.getElementById('dateBox').innerHTML = html;
var curHour = this.o.hour;
var curMinute = this.o.minute;
var curSecond = this.o.second;
var curTime = curHour + ':' + curMinute + ':' + curSecond;
//上月
var prevMonthDate = this.getPrevMonthDate();
prevMonthDate += ' ' + curTime;
document.getElementById('prevMonth').setAttribute('onclick', 'dateAction("' + prevMonthDate + '");');
//下月
var nextMonthDate = this.getNextMonthDate();
nextMonthDate += ' ' + curTime;
document.getElementById('nextMonth').setAttribute('onclick', 'dateAction("' + nextMonthDate + '");');
//上年
var prevYearDate = this.getPrevYearDate();
prevYearDate += ' ' + curTime;
document.getElementById('prevYear').setAttribute('onclick', 'dateAction("' + prevYearDate + '");');
//下年
var nextYearDate = this.getNextYearDate();
nextYearDate += ' ' + curTime;
document.getElementById('nextYear').setAttribute('onclick', 'dateAction("' + nextYearDate + '");');
//实时时间
var curFullDate = this.o.year + '-' + this.o.month + '-' + this.o.day + ' ' + this.o.hour + ':' + this.o.minute + ':' + this.o.second;
showActualDate(curDate, curFullDate);
}
//下年日期
this.getNextYearDate = function() {
var nextYear = this.o.year + 1;
var nextYearDate = nextYear + '-' + this.o.month;
var nextObj = new dateRender(nextYearDate + '-1');
var nextYearMonthLastDay = nextObj.getMonthLastDay();
nextYearMonthLastDay = parseInt(nextYearMonthLastDay.day);
var nextYearCurDay = this.o.day > nextYearMonthLastDay ? nextYearMonthLastDay : this.o.day;
return nextYearDate + '-' + nextYearCurDay;
}
//上年日期
this.getPrevYearDate = function() {
var prevYear = this.o.year - 1;
var prevYearDate = prevYear + '-' + this.o.month;
var prevObj = new dateRender(prevYearDate + '-1');
var prevYearMonthLastDay = prevObj.getMonthLastDay();
prevYearMonthLastDay = parseInt(prevYearMonthLastDay.day);
var prevYearCurDay = this.o.day > prevYearMonthLastDay ? prevYearMonthLastDay : this.o.day;
return prevYearDate + '-' + prevYearCurDay;
}
//上月日期
this.getPrevMonthDate = function() {
var prevMothDay = this.getPrevMonthDay();
var prevDate = prevMothDay.date.split('-');
var prevDay = parseInt(prevDate[2]);
prevDay = this.o.day > prevDay ? prevDay : this.o.day;
prevDate[2] = prevDay;
return prevDate.join('-');
}
//下月日期
this.getNextMonthDate = function() {
var nextMonthDate = this.getNextMonthDay();
nextMonthDate = nextMonthDate.split('-');
var nextMonthlastDay = parseInt(nextMonthDate[2]);
nextMonthlastDay = this.o.day > nextMonthlastDay ? nextMonthlastDay : this.o.day;
nextMonthDate[2] = nextMonthlastDay;
return nextMonthDate.join('-');
}
//本月第一次是周几
this.getMonthFirstWeek = function() {
var firstDate = this.o.year + '-' + this.o.month + '-1';
var forstObj = new Date(firstDate);
return {
date: firstDate,
week: forstObj.getDay()
};
}
//当月最后一天
this.getMonthLastDay = function() {
if(this.o.month == 12) {
var year = this.o.year + 1;
var month = 1;
} else {
var year = this.o.year;
var month = this.o.month + 1;
}
var nextData = year + '-' + month + '-1';
var dateObj = new Date(nextData);
var nextMonthFirstDayTime = dateObj.getTime(); //下个月一号对应毫秒
var theMonthLastDayTime = nextMonthFirstDayTime - (24 * 60 * 60 * 1000); //下个月一号减去一天,正好是这个月最后一天
var monthLastDay = (new Date(theMonthLastDayTime)).getDate();
return {
date: this.o.year + '-' + this.o.month + '-' + monthLastDay,
day: monthLastDay
};
}
//上月最后一天
this.getPrevMonthDay = function() {
if(this.o.month == 1) {
var prevYear = this.o.year - 1;
var prevMonth = 12;
} else {
var prevYear = this.o.year;
var prevMonth = this.o.month - 1;
}
var prevData = this.o.year + '-' + this.o.month + '-1'; //当月1号
var dateObj = new Date(prevData);
var curMonthFirstDayTime = dateObj.getTime(); //当月一号对应毫秒
var prevMonthLastDayTime = curMonthFirstDayTime - (24 * 60 * 60 * 1000); //当月1号减去一天,得到上月最后一天
var monthPrevDay = (new Date(prevMonthLastDayTime)).getDate();
return {
date: prevYear + '-' + prevMonth + '-' + monthPrevDay,
day: monthPrevDay
};
}
//下月最后一天
this.getNextMonthDay = function() {
if(this.o.month == 12) {
var nextYear = this.o.year + 1;
var nextMonth = 1;
} else {
var nextYear = this.o.year;
var nextMonth = this.o.month + 1;
}
var nextDate = nextYear + '-' + nextMonth + '-1';
var nextObj = new dateRender(nextDate);
var nextDateObj = nextObj.getMonthLastDay();
return nextYear + '-' + nextMonth + '-' + nextDateObj.day;
}
this.getFullDays = function() {
var dates = [];
var monthFirstWeek = this.getMonthFirstWeek(); //当月第一天是周几
var monthLastDay = this.getMonthLastDay(); //当前月最后一天
if(monthFirstWeek.week != 1) {
var prevMonthDay = this.getPrevMonthDay(); //上月最后一天
var sub = monthFirstWeek.week == 0 ? 6 : monthFirstWeek.week - 1;
var start = prevMonthDay.day - sub + 1;
var prveDate = prevMonthDay.date;
prveDate = prveDate.split('-');
prveDate = prveDate[0] + '-' + prveDate[1] + '-';
for(var i = start; i <= prevMonthDay.day; i++) {
dates[dates.length] = {
date: prveDate + i,
day: getFmtNun(i),
type: 'prev'
};
}
}
var curDate = monthLastDay.date;
curDate = curDate.split('-');
curDate = curDate[0] + '-' + curDate[1] + '-';
for(var i = 1; i <= monthLastDay.day; i++) {
dates[dates.length] = {
date: curDate + i,
day: getFmtNun(i),
type: 'cur'
};
}
return dates;
}
}
function dateAction(date) {
var o = new dateRender(date);
o.render();
}
function getDayActualDate(dayDate) {
var o = new getDate();
var fullDate = dayDate + ' ' + o.hour + ':' + o.minute + ':' + o.second;
dateAction(fullDate);
}
//实时显示时间
showActualDate = function(curDate, curFullDate) {
actualCmd = window.setTimeout(function() {
var obj = new Date(curFullDate);
var nextSecond = obj.getTime() + 1000;
var nextObj = new Date(nextSecond);
var nextDate = nextObj.getFullYear() + '-' + (nextObj.getMonth() + 1) + '-' + nextObj.getDate();
var nextTime = getFmtNun(nextObj.getHours()) + ':' + getFmtNun(nextObj.getMinutes()) + ':' + getFmtNun(nextObj.getSeconds());
var nextFullDate = nextDate + ' ' + nextTime;
if(curDate == nextDate) {
document.getElementById('time').innerHTML = nextTime;
} else {
dateAction(nextFullDate);
return false;
}
console.log(nextFullDate)
showActualDate(curDate, nextFullDate);
}, 985);
}
window.onload = function() {
dateAction();
}
</script>
</head>
<body>
<section class="main">
<div class="date-box yeah-box">
<div>
<a href="javascript:;" id="prevYear"><上年</a>
</div>
<div>
<a href="javascript:;" id="prevMonth"><上月</a>
</div>
<div class="yeah" id="yeah">
</div>
<div>
<a href="javascript:;" id="nextMonth">下月></a>
</div>
<div>
<a href="javascript:;" id="nextYear">下年></a>
</div>
</div>
<div class="date-box" id="date">
</div>
<div class="date-box cur-time" id="time">
</div>
<ul class="date-box date-line week-line">
<li data-id="1">周一</li>
<li data-id="2">周二</li>
<li data-id="3">周三</li>
<li data-id="4">周四</li>
<li data-id="5">周五</li>
<li data-id="6">周六</li>
<li data-id="0">周日</li>
</ul>
<ul class="date-box date-line" id="dateBox">
</ul>
<div class="date-box">
<a href="javascript:;" onclick="dateAction();" class="cur-time-btn">当前时间</a>
</div>
</section>
</body>
</html>
相关推荐
© 2020 asciim码
人生就是一场修行