ASCII码 ASCII码

js对象、函数、日期对象----简易日历

发布于:2022-05-20 18:41:07  栏目:技术文档

虽然搞了很久,出来后成就感杠杠的~

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>日历</title>
  6. <style>
  7. body{
  8. font-size: 12px;
  9. }
  10. .main{
  11. width: 520px;
  12. margin: 0 auto;
  13. margin-top: 50px;
  14. border: 1px solid #CCC;
  15. display: flex;
  16. flex-direction: column;
  17. justify-content: flex-start;
  18. align-items: center;
  19. color: #333;
  20. padding: 5px 0;
  21. box-sizing: border-box;
  22. border-radius: 5px;
  23. }
  24. a{
  25. text-decoration: none;
  26. color: #333;
  27. }
  28. a:hover{
  29. text-decoration: underline;
  30. }
  31. div{
  32. text-align: center;
  33. }
  34. .main div.date-box{
  35. width: 100%;
  36. }
  37. .main .yeah-box{
  38. display: flex;
  39. justify-content: space-between;
  40. align-items: center;
  41. }
  42. .main .yeah-box div{
  43. height: 40px;
  44. line-height: 40px;
  45. }
  46. .main .yeah-box div.yeah{
  47. flex-grow: 1;
  48. font-size: 14px;
  49. }
  50. .main .yeah-box div:not(.yeah){
  51. padding: 0 10px;
  52. }
  53. .main .cur-time{
  54. font-size: 16px;
  55. height: 40px;
  56. line-height: 40px;
  57. font-weight: bold;
  58. }
  59. .main .date-line{
  60. display: flex;
  61. justify-content: flex-start;
  62. flex-wrap: wrap;
  63. list-style: none;
  64. padding: 0;
  65. margin: 0;
  66. width: 97.5%;
  67. padding-left: 2.5%;
  68. }
  69. .main .week-line{
  70. margin-bottom: 10px;
  71. }
  72. .main .date-line li{
  73. list-style: none;
  74. padding: 0;
  75. width: 70px;
  76. text-align: center;
  77. height: 35px;
  78. line-height: 35px;
  79. }
  80. .main .date-line:not(.week-line) li{
  81. cursor: pointer;
  82. margin-bottom: 5px;
  83. }
  84. .main .date-line:not(.week-line) li:not(.disable):hover{
  85. background-color: #666;
  86. color: #FFF;
  87. border-radius: 5px;
  88. }
  89. .main .date-line:not(.week-line) li.active{
  90. background-color: #666;
  91. color: #FFF;
  92. border-radius: 5px;
  93. }
  94. .main .date-line li.disable{
  95. cursor: default;
  96. color: #DDD;
  97. }
  98. .cur-time-btn{
  99. display: block;
  100. background-color: #666;
  101. width: 20%;
  102. margin: 0 auto;
  103. padding: 7px 0;
  104. color: #FFF;
  105. border-radius:5px;
  106. margin-bottom: 10px;
  107. }
  108. </style>
  109. <script>
  110. function getFmtNun(num) {
  111. num = num.toString();
  112. return num.length == 1 ? '0' + num : num;
  113. }
  114. function getWeekStr(week) {
  115. var strs = ['日', '一', '二', '三', '四', '五', '六'];
  116. return strs[week];
  117. }
  118. function getDate(date = '') {
  119. if(date == '') {
  120. var myDate = new Date();
  121. } else {
  122. var myDate = new Date(date);
  123. }
  124. this.year = myDate.getFullYear();
  125. this.month = myDate.getMonth() + 1;
  126. this.monthStr = getFmtNun(this.month);
  127. this.day = myDate.getDate();
  128. this.dayStr = getFmtNun(this.day);
  129. this.week = myDate.getDay();
  130. this.weekStr = getWeekStr(this.week);
  131. this.hour = myDate.getHours();
  132. this.hourStr = getFmtNun(this.hour);
  133. this.minute = myDate.getMinutes();
  134. this.minuteStr = getFmtNun(this.minute);
  135. this.second = myDate.getSeconds();
  136. this.secondStr = getFmtNun(this.second);
  137. return this;
  138. }
  139. var actualCmd;
  140. function dateRender(date = '') {
  141. window.clearTimeout(actualCmd);
  142. this.o = new getDate(date);
  143. this.render = function() {
  144. var curYear = this.o.year;
  145. var curMonth = this.o.month;
  146. var curDay = this.o.day;
  147. var curDate = curYear + '-' + curMonth + '-' + curDay;
  148. document.getElementById('yeah').innerHTML = curYear + '年';
  149. document.getElementById('date').innerHTML = this.o.monthStr + '月' + this.o.dayStr + '日 周' + this.o.weekStr;
  150. document.getElementById('time').innerHTML = this.o.hourStr + ':' + this.o.minuteStr + ':' + this.o.secondStr;
  151. //单天选择
  152. var fullDates = this.getFullDays();
  153. var html = '';
  154. for(var key in fullDates) {
  155. var click = fullDates[key].type == 'prev' ? '' : 'getDayActualDate(\'' + fullDates[key].date + '\');';
  156. 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>';
  157. html += tab;
  158. }
  159. document.getElementById('dateBox').innerHTML = html;
  160. var curHour = this.o.hour;
  161. var curMinute = this.o.minute;
  162. var curSecond = this.o.second;
  163. var curTime = curHour + ':' + curMinute + ':' + curSecond;
  164. //上月
  165. var prevMonthDate = this.getPrevMonthDate();
  166. prevMonthDate += ' ' + curTime;
  167. document.getElementById('prevMonth').setAttribute('onclick', 'dateAction("' + prevMonthDate + '");');
  168. //下月
  169. var nextMonthDate = this.getNextMonthDate();
  170. nextMonthDate += ' ' + curTime;
  171. document.getElementById('nextMonth').setAttribute('onclick', 'dateAction("' + nextMonthDate + '");');
  172. //上年
  173. var prevYearDate = this.getPrevYearDate();
  174. prevYearDate += ' ' + curTime;
  175. document.getElementById('prevYear').setAttribute('onclick', 'dateAction("' + prevYearDate + '");');
  176. //下年
  177. var nextYearDate = this.getNextYearDate();
  178. nextYearDate += ' ' + curTime;
  179. document.getElementById('nextYear').setAttribute('onclick', 'dateAction("' + nextYearDate + '");');
  180. //实时时间
  181. var curFullDate = this.o.year + '-' + this.o.month + '-' + this.o.day + ' ' + this.o.hour + ':' + this.o.minute + ':' + this.o.second;
  182. showActualDate(curDate, curFullDate);
  183. }
  184. //下年日期
  185. this.getNextYearDate = function() {
  186. var nextYear = this.o.year + 1;
  187. var nextYearDate = nextYear + '-' + this.o.month;
  188. var nextObj = new dateRender(nextYearDate + '-1');
  189. var nextYearMonthLastDay = nextObj.getMonthLastDay();
  190. nextYearMonthLastDay = parseInt(nextYearMonthLastDay.day);
  191. var nextYearCurDay = this.o.day > nextYearMonthLastDay ? nextYearMonthLastDay : this.o.day;
  192. return nextYearDate + '-' + nextYearCurDay;
  193. }
  194. //上年日期
  195. this.getPrevYearDate = function() {
  196. var prevYear = this.o.year - 1;
  197. var prevYearDate = prevYear + '-' + this.o.month;
  198. var prevObj = new dateRender(prevYearDate + '-1');
  199. var prevYearMonthLastDay = prevObj.getMonthLastDay();
  200. prevYearMonthLastDay = parseInt(prevYearMonthLastDay.day);
  201. var prevYearCurDay = this.o.day > prevYearMonthLastDay ? prevYearMonthLastDay : this.o.day;
  202. return prevYearDate + '-' + prevYearCurDay;
  203. }
  204. //上月日期
  205. this.getPrevMonthDate = function() {
  206. var prevMothDay = this.getPrevMonthDay();
  207. var prevDate = prevMothDay.date.split('-');
  208. var prevDay = parseInt(prevDate[2]);
  209. prevDay = this.o.day > prevDay ? prevDay : this.o.day;
  210. prevDate[2] = prevDay;
  211. return prevDate.join('-');
  212. }
  213. //下月日期
  214. this.getNextMonthDate = function() {
  215. var nextMonthDate = this.getNextMonthDay();
  216. nextMonthDate = nextMonthDate.split('-');
  217. var nextMonthlastDay = parseInt(nextMonthDate[2]);
  218. nextMonthlastDay = this.o.day > nextMonthlastDay ? nextMonthlastDay : this.o.day;
  219. nextMonthDate[2] = nextMonthlastDay;
  220. return nextMonthDate.join('-');
  221. }
  222. //本月第一次是周几
  223. this.getMonthFirstWeek = function() {
  224. var firstDate = this.o.year + '-' + this.o.month + '-1';
  225. var forstObj = new Date(firstDate);
  226. return {
  227. date: firstDate,
  228. week: forstObj.getDay()
  229. };
  230. }
  231. //当月最后一天
  232. this.getMonthLastDay = function() {
  233. if(this.o.month == 12) {
  234. var year = this.o.year + 1;
  235. var month = 1;
  236. } else {
  237. var year = this.o.year;
  238. var month = this.o.month + 1;
  239. }
  240. var nextData = year + '-' + month + '-1';
  241. var dateObj = new Date(nextData);
  242. var nextMonthFirstDayTime = dateObj.getTime(); //下个月一号对应毫秒
  243. var theMonthLastDayTime = nextMonthFirstDayTime - (24 * 60 * 60 * 1000); //下个月一号减去一天,正好是这个月最后一天
  244. var monthLastDay = (new Date(theMonthLastDayTime)).getDate();
  245. return {
  246. date: this.o.year + '-' + this.o.month + '-' + monthLastDay,
  247. day: monthLastDay
  248. };
  249. }
  250. //上月最后一天
  251. this.getPrevMonthDay = function() {
  252. if(this.o.month == 1) {
  253. var prevYear = this.o.year - 1;
  254. var prevMonth = 12;
  255. } else {
  256. var prevYear = this.o.year;
  257. var prevMonth = this.o.month - 1;
  258. }
  259. var prevData = this.o.year + '-' + this.o.month + '-1'; //当月1号
  260. var dateObj = new Date(prevData);
  261. var curMonthFirstDayTime = dateObj.getTime(); //当月一号对应毫秒
  262. var prevMonthLastDayTime = curMonthFirstDayTime - (24 * 60 * 60 * 1000); //当月1号减去一天,得到上月最后一天
  263. var monthPrevDay = (new Date(prevMonthLastDayTime)).getDate();
  264. return {
  265. date: prevYear + '-' + prevMonth + '-' + monthPrevDay,
  266. day: monthPrevDay
  267. };
  268. }
  269. //下月最后一天
  270. this.getNextMonthDay = function() {
  271. if(this.o.month == 12) {
  272. var nextYear = this.o.year + 1;
  273. var nextMonth = 1;
  274. } else {
  275. var nextYear = this.o.year;
  276. var nextMonth = this.o.month + 1;
  277. }
  278. var nextDate = nextYear + '-' + nextMonth + '-1';
  279. var nextObj = new dateRender(nextDate);
  280. var nextDateObj = nextObj.getMonthLastDay();
  281. return nextYear + '-' + nextMonth + '-' + nextDateObj.day;
  282. }
  283. this.getFullDays = function() {
  284. var dates = [];
  285. var monthFirstWeek = this.getMonthFirstWeek(); //当月第一天是周几
  286. var monthLastDay = this.getMonthLastDay(); //当前月最后一天
  287. if(monthFirstWeek.week != 1) {
  288. var prevMonthDay = this.getPrevMonthDay(); //上月最后一天
  289. var sub = monthFirstWeek.week == 0 ? 6 : monthFirstWeek.week - 1;
  290. var start = prevMonthDay.day - sub + 1;
  291. var prveDate = prevMonthDay.date;
  292. prveDate = prveDate.split('-');
  293. prveDate = prveDate[0] + '-' + prveDate[1] + '-';
  294. for(var i = start; i <= prevMonthDay.day; i++) {
  295. dates[dates.length] = {
  296. date: prveDate + i,
  297. day: getFmtNun(i),
  298. type: 'prev'
  299. };
  300. }
  301. }
  302. var curDate = monthLastDay.date;
  303. curDate = curDate.split('-');
  304. curDate = curDate[0] + '-' + curDate[1] + '-';
  305. for(var i = 1; i <= monthLastDay.day; i++) {
  306. dates[dates.length] = {
  307. date: curDate + i,
  308. day: getFmtNun(i),
  309. type: 'cur'
  310. };
  311. }
  312. return dates;
  313. }
  314. }
  315. function dateAction(date) {
  316. var o = new dateRender(date);
  317. o.render();
  318. }
  319. function getDayActualDate(dayDate) {
  320. var o = new getDate();
  321. var fullDate = dayDate + ' ' + o.hour + ':' + o.minute + ':' + o.second;
  322. dateAction(fullDate);
  323. }
  324. //实时显示时间
  325. showActualDate = function(curDate, curFullDate) {
  326. actualCmd = window.setTimeout(function() {
  327. var obj = new Date(curFullDate);
  328. var nextSecond = obj.getTime() + 1000;
  329. var nextObj = new Date(nextSecond);
  330. var nextDate = nextObj.getFullYear() + '-' + (nextObj.getMonth() + 1) + '-' + nextObj.getDate();
  331. var nextTime = getFmtNun(nextObj.getHours()) + ':' + getFmtNun(nextObj.getMinutes()) + ':' + getFmtNun(nextObj.getSeconds());
  332. var nextFullDate = nextDate + ' ' + nextTime;
  333. if(curDate == nextDate) {
  334. document.getElementById('time').innerHTML = nextTime;
  335. } else {
  336. dateAction(nextFullDate);
  337. return false;
  338. }
  339. console.log(nextFullDate)
  340. showActualDate(curDate, nextFullDate);
  341. }, 985);
  342. }
  343. window.onload = function() {
  344. dateAction();
  345. }
  346. </script>
  347. </head>
  348. <body>
  349. <section class="main">
  350. <div class="date-box yeah-box">
  351. <div>
  352. <a href="javascript:;" id="prevYear"><上年</a>
  353. </div>
  354. <div>
  355. <a href="javascript:;" id="prevMonth"><上月</a>
  356. </div>
  357. <div class="yeah" id="yeah">
  358. </div>
  359. <div>
  360. <a href="javascript:;" id="nextMonth">下月></a>
  361. </div>
  362. <div>
  363. <a href="javascript:;" id="nextYear">下年></a>
  364. </div>
  365. </div>
  366. <div class="date-box" id="date">
  367. </div>
  368. <div class="date-box cur-time" id="time">
  369. </div>
  370. <ul class="date-box date-line week-line">
  371. <li data-id="1">周一</li>
  372. <li data-id="2">周二</li>
  373. <li data-id="3">周三</li>
  374. <li data-id="4">周四</li>
  375. <li data-id="5">周五</li>
  376. <li data-id="6">周六</li>
  377. <li data-id="0">周日</li>
  378. </ul>
  379. <ul class="date-box date-line" id="dateBox">
  380. </ul>
  381. <div class="date-box">
  382. <a href="javascript:;" onclick="dateAction();" class="cur-time-btn">当前时间</a>
  383. </div>
  384. </section>
  385. </body>
  386. </html>
相关推荐
阅读 +