This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //先算出本月1號是星期幾 | |
| let today=new Date(); | |
| let thisYear=today.getFullYear(); | |
| let thisMonth=today.getMonth(); | |
| //建立今年1月1日的日期物件 | |
| let firstDayofMonth=new Date(thisYear,thisMonth,1); | |
| //本月1號是星期幾 | |
| let weekDayofFirstdayofMonth=firstDayofMonth.getDay(); | |
| let monthDays=[31,28,31,30,31,30,31,31,30,31,30,31]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //填滿上個月的日期直到Days[0] | |
| for(let i=weekDayofFirstdayofMonth-1,d=monthDays[thisMonth-1];i>=0;i--,d--){ | |
| days[i].innerHTML=d; | |
| days[i].classList.add("color"); //為其添加css類別來上色,方便跟本月作出區隔 | |
| } | |
| //填滿下個月的日期直到Days[41] | |
| for(let i=weekDayofFirstdayofMonth+monthDays[thisMonth],d=1;i<42;i++,d++){ | |
| days[i].innerHTML=d; | |
| days[i].classList.add("color");//為其添加css類別來上色,方便跟本月作出區隔 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //為今天加點色彩 | |
| let thisDay=today.getDate(); | |
| days[thisDay-1].setAttribute("id","current-day"); | |
| //由於今天是29號(發文當天日期),位於td[28]裡,所以後面要減1。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //擷取HTML文件中所有td元素,返回所有td元素的集合-陣列 | |
| let days=document.getElementsByTagName("td"); | |
| //去除先前"current-day"的屬性設定 | |
| if (document.getElementById("current-day")) { | |
| document.getElementById("current-day").removeAttribute("id"); | |
| } | |
| //填滿本月的日期 | |
| for(let i=1;i<=monthDays[thisMonth];i++){ | |
| days[weekDayofFirstdayofMonth+i-1].innerHTML=i; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Month | |
| document.getElementById("cur-month").innerHTML = getMonthName(today.getMonth()); | |
| document.getElementById("cal_month").innerHTML = getMonthName(today.getMonth()); | |
| //Day 星期幾(這裡有呼叫函式) | |
| document.getElementById("cur-day").innerHTML = getWeekName(today.getDay()); | |
| //Date 日期 | |
| document.getElementById("cur-date").innerHTML = addDateOrdinal(today.getDate()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function addDateOrdinal(date) { //加上日期序數 | |
| switch (date) { | |
| case 1: | |
| case 21: | |
| case 31: return date + "<sup>st</sup>"; | |
| case 2: | |
| case 22: return date + "<sup>nd</sup>"; | |
| case 3: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getMonthName(month){ | |
| var monthNames=["January","February","March","April","May","June","July","August","September","October","November","December"]; | |
| return monthNames[month]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script language="JavaScript"> | |
| function getWeekName(day){ | |
| if(day==0) {return "Sunday";} //如果區塊內只有單一敘述,可以省略"{}" | |
| if(day==1) return "Monday"; | |
| if(day==2) return "Tuesday"; | |
| if(day==3) return "Wednesday"; | |
| if(day==4) return "Thursday"; | |
| if(day==5) return "Friday"; | |
| if(day==6) return "Saturday"; | |
| //return 從函式返回 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script language="JavaScript"> | |
| var today=new Date(); | |
| //Year | |
| document.getElementById("background-year").innerHTML=today.getFullYear(); | |
| document.getElementById("cur-year").innerHTML=today.getFullYear(); | |
| document.getElementById("cal_year").innerHTML=today.getFullYear(); | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script language=“JavaScript”> | |
| document.getElementById(“id”).innerHTML = new Date().getDate(); | |
| </script> |
NewerOlder