Dasar JavaScript Kalender
One of the things that you can do with Javascript is create a calendar that will automatically show the entire month, and highlight the current day of the month. Salah satu hal yang dapat Anda lakukan dengan Javascript adalah membuat kalender yang secara otomatis akan menampilkan seluruh bulan, dan menyoroti hari saat bulan. This may seem like a complicated task to do, but if we just break down what we need to have in order to make a monthly calendar it is not that hard of a task. Hal ini mungkin tampak seperti tugas yang rumit untuk melakukannya, tetapi jika kita sekadar memecah apa yang perlu kita miliki untuk membuat kalender bulanan itu tidak sulit tugas.First, let's look at what variables are needed for the calendar. Pertama, mari kita lihat apa variabel yang diperlukan untuk kalender. We know we need the current month, day, and year. Kita tahu kita perlu bulan berjalan, hari, dan tahun. These are simple to get with date functions in JavaScript. Ini adalah sederhana untuk mendapatkan dengan fungsi tanggal dalam JavaScript. Here is how we would get those variables. Berikut adalah bagaimana kita akan mendapatkan variabel-variabel.
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var current = new Date();</span> saat var Tanggal = baru ();</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var month = current.getMonth();</span> var bulan = current.getMonth ();</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var day = current.getDate();</span> var hari = current.getDate ();</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var year = current.getFullYear();</span> var tahun = current.getFullYear ();</span>
Now that we have the current date, we need to find a few other things out, like what day of the week does the first day of the month fall on. Sekarang bahwa kita memiliki tanggal saat ini, kita perlu mencari beberapa hal lainnya keluar, seperti apa hari dalam seminggu tidak hari pertama musim gugur bulan pada. We need to know this information so that we know where to start the first day in the calendar, if not then every month will have its first day of the week start on a Sunday, and we all know that is not correct. Kita perlu tahu informasi ini sehingga kita tahu di mana untuk memulai hari pertama dalam kalender, jika tidak maka setiap bulan akan memiliki hari pertama awal minggu pada hari Minggu, dan kita semua tahu bahwa tidak benar.
To get the day of the week that the month starts on, we need to set the date by using the variables we just created. Untuk mendapatkan hari minggu bulan itu dimulai pada, kita perlu menyetel tanggal dengan menggunakan variabel-variabel baru saja kita buat. But before we do that we have to add one additional variable that will allow us to get the correct month. Tapi sebelum kita melakukan itu kita harus menambahkan satu variabel tambahan yang akan memungkinkan kita untuk mendapatkan bulan yang benar.
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">tempMonth = month +1;</span> tempMonth = bulan +1;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var tempDate = new Date(tempMonth +' 1 ,'+year);</span> var tempDate Tanggal = baru (tempMonth + '1,' + tahun);</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var tempweekday= tempDate.getDay();</span> var tempweekday = tempDate.getDay ();</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var tempweekday2 = tempweekday;</span> var tempweekday2 = tempweekday;</span>
We now need to get how many days there are in the month, that way when we create our loop we have a way to tell the JavaScript to stop the loop. Kita sekarang perlu untuk mendapatkan berapa hari ada di bulan, cara bahwa ketika kita membuat loop kami, kami memiliki cara untuk memberitahu JavaScript untuk menghentikan loop. We can do this with an array. Kita dapat melakukan ini dengan array. In the array we will have all the total days in a month, but this raises an issue. Dalam array kita akan memiliki semua total hari dalam sebulan, tapi ini menimbulkan masalah. February the number of days can change depending on if it is a leap year or not. Februari jumlah hari dapat berubah tergantung pada apakah itu adalah tahun kabisat atau tidak. This is an easy way to solve this issue. Ini adalah cara mudah untuk memecahkan masalah ini. To find out if it is a leap year, we just need to divide the year by four and check to see if there are any remainder.Untuk mengetahui jika itu adalah tahun kabisat, kita hanya perlu membagi tahun dengan empat dan memeriksa untuk melihat apakah ada sisa. Sounds simple enough, but we also have to remember that every 100 years if the year is not divisible by 400, then it is not a leap year. Kedengarannya cukup sederhana, namun kita juga harus ingat bahwa setiap 100 tahun jika tahun tidak habis dibagi oleh 400, maka itu bukanlah tahun kabisat. An example of this is the year 1900. Sebuah contoh dari hal ini adalah tahun 1900. It should have been a leap year, but since it was not divisible by 400, it was not a leap year where as 2000 was. Seharusnya tahun kabisat, tetapi karena tidak dibagi dengan 400, itu bukan tahun kabisat dimana tahun 2000. To get check for this type of leap year, we need to divide the year by 100, and if there is no remainder then divide the year by 400. Untuk mendapatkan cek untuk jenis tahun kabisat, kita perlu membagi tahun dengan 100, dan jika ada sisa tidak maka membagi tahun dengan 400. Once we get this information, we can then get create the array. Setelah kita mendapatkan informasi ini, kita kemudian bisa mendapatkan menciptakan array.
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">if (month == 1){</span> jika (bulan == 1) {</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">if ( (year%100!=0) && (year%4==0) || (year%400==0)){</span> jika ((tahun% 100 = 0) & & (tahun% 4 == 0) | | (tahun% 400 == 0)) {</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">totalFeb = 29;</span> totalFeb = 29;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">}else{</span> } Else {</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">totalFeb = 28;</span> totalFeb = 28;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">}</span> }</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">}</span> }</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var totalDays = ["31", ""+totalFeb+"","31","30","31","30","31","31","30","31","30","31"];</span> var totalDays = ["31", "" + totalFeb +""," 31 "," 30 "," 31 "," 30 "," 31 "," 31 "," 30 "," 31 "," 30 " , "31"];</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var padding= "";</span> Padding var = "";</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">while (tempweekday > 0)</span> sementara (tempweekday> 0)</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">padding += "</span> Padding + = "</span> <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">"; tempweekday--; }</span> "; Tempweekday -;}</span>
Now we will use the array that we created, and the copy of tempweekday to create the rest of the calendar. Sekarang kita akan menggunakan array yang kita buat, dan salinan tempweekday untuk menciptakan sisa dari kalender.
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">dayAmount = totalDays[month];</span> dayAmount = totalDays [bulan];</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">i = "0";</span> i = "0";</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">while (i <= dayAmount){</span> sementara (i <= dayAmount) {</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">if (tempweekday2 > 6){</span> jika (tempweekday2> 6) {</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">tempweekday2 = 0;</span> tempweekday2 = 0;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">padding += "</tr><tr>";</span> Padding + = "</ tr> <tr>";</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">}</span> }</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">if (i == day){</span> jika (i == hari) {</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">padding +="<td class='currentday'>"+i+"</td>";</span> Padding + = "<td class='currentday'>" + i + "</ td>";</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">}else{</span> } Else {</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">padding +="<td class='currentmonth'>"+i+"</td>";</span> Padding + = "<td class='currentmonth'>" + i + "</ td>";</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">}</span> }</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">tempweekday2++;</span> tempweekday2 + +;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">i++;</span> i + +;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">}</span> }</span>
The next if statement will is what actually displays the number for the days. Berikutnya jika pernyataan adalah apa yang sebenarnya akan menampilkan nomor untuk hari. In order to make the current day of the month stand out, we need this statement. Dalam rangka untuk membuat hari saat bulan menonjol, kita perlu pernyataan ini. What it is doing is checking to see if our counter (i) is equal to the current day that we got at the start of the script. Apa yang dilakukannya adalah memeriksa untuk melihat apakah counter kami (i) adalah sama dengan hari ini yang kita peroleh pada awal script. If so it is going to apply a different CSS class to that cell. Jika demikian itu akan menerapkan kelas CSS yang berbeda untuk sel tersebut. If the counter and day are not equal then we create a cell with a different CSS class. Jika counter dan hari tidak sama maka kita membuat sebuah sel dengan kelas CSS yang berbeda. At the end we add one to both the tempweekday2 and i values. Pada akhirnya kami menambahkan satu untuk kedua tempweekday2 dan saya nilai.
Now that we have the body of the calendar created, we need to create the header of the calendar. Sekarang bahwa kita memiliki tubuh kalender dibuat, kita perlu menciptakan header kalender. This will be simple to do, it is all HTML. Ini akan menjadi sederhana untuk dilakukan, itu semua HTML. In the calendar's header we want to display the name of the month, instead of a number. Dalam header kalender kita ingin menampilkan nama bulan, bukan nomor. To do this we will create an array. Untuk melakukan ini kita akan membuat sebuah array. Just like we did for the number of days in the month, we will use the month variable to pull the month name from the array. Sama seperti yang kita lakukan untuk jumlah hari dalam sebulan, kita akan menggunakan variabel bulan untuk menarik nama bulan dari array.
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var monthNames = ["Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","Nov", "Dec"];</span> monthNames var = ["Jan", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember "];</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">var calendarTable = "<table class='calendar'> <tr class='currentmonth'><th colspan='7'>"+monthNames[month]+" "+ year +"</th></tr>";</span> calendarTable var = "<table class='calendar'> <tr class='currentmonth'> <th colspan='7'>" + monthNames [bulan] + "" + tahun + "</ th> </ tr>" ;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">calendarTable +="<tr class='weekdays'> <td>Sun</td> <td>Mon</td> <td>Tues</td> <td>Wed</td> <td>Thurs</td> <td>Fri</td> <td>Sat</td> </tr>";</span> calendarTable + = "<tr class='weekdays'> <td> Sun </ td> <td> Sen </ td> <td> Sel </ td> <td> Rabu </ td> <td> Kam </ td> <td> Jum </ td> <td> Sab </ td> </ tr> ";</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">calendarTable += "<tr>";</span> calendarTable + = "tr";</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">calendarTable += padding;</span> calendarTable + = bantalan;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">calendarTable += "</tr></table>" + month;</span> calendarTable + = "</ tr> </ table>" + bulan;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">document.getElementById("calendar").innerHTML=calendarTable;</span> . document.getElementById ("kalender") innerHTML = calendarTable;</span>
In the loop that we created to make fill in the calendar with the number of days in the month, we put the current day in a different CSS class then the other days of the month. Dalam loop yang kita buat untuk membuat mengisi kalender dengan jumlah hari dalam sebulan, kami menempatkan hari ini di kelas CSS yang berbeda maka hari-hari lain bulan. This was to distinguish the current day of the month, and make it stand out. Ini adalah untuk membedakan hari saat bulan, dan membuatnya menonjol. This is what that CSS looks like. Ini adalah apa yang terlihat seperti CSS.
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;"><style></span> <style></span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">.currentmonth{</span> . Currentmonth {</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">color: blue;</span> warna: biru;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">text-align: center;</span> text-align: center;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">}</span> }</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">.currentday{</span> . Currentday {</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">border: 1px solid black;</span> border: 1px hitam pekat;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">color: #00FF00;</span> color: # 00FF00;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">text-align: center;</span> text-align: center;</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;">}</span> }</span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;"></style></span> </ Style></span>
- <span onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left;"><style> .currentmonth{ color: blue; text-align: center; } .currentday{ border: 1px solid black; color: #00FF00; text-align: center; } </style> <script language="JavaScript"> function calendar(){ var current = new Date(); var month = current.getMonth(); var day = current.getDate(); var year = current.getFullYear(); tempMonth = month +1; var tempDate = new Date(tempMonth +' 1 ,'+year); var tempweekday= tempDate.getDay(); var tempweekday2 = tempweekday; if (month == 1){ if ( (year%100!=0) && (year%4==0) || (year%400==0)){ totalFeb = 29; }else{ totalFeb = 28; } } var totalDays = ["31", ""+totalFeb+"","31","30","31","30","31","31","30","31","30","31"]; var padding= ""; while (tempweekday > 0) padding += "<td class='premonth'></td>"; tempweekday--; } dayAmount = totalDays[month]; i = "0"; while (i <= dayAmount){ if (tempweekday2 > 6){ tempweekday2 = 0; padding += "</tr><tr>"; } if (i == day){ padding +="<td class='currentday'>"+i+"</td>"; }else{ padding +="<td class='currentmonth'>"+i+"</td>"; } tempweekday2++; i++; } var monthNames = ["Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","Nov", "Dec"]; var calendarTable = "<table class='calendar'> <tr class='currentmonth'><th colspan='7'>"+monthNames[month]+" "+ year +"</th></tr>"; calendarTable +="<tr class='weekdays'> <td>Sun</td> <td>Mon</td> <td>Tues</td> <td>Wed</td> <td>Thurs</td> <td>Fri</td> <td>Sat</td> </tr>"; calendarTable += "<tr>"; } </script></span> <style> currentmonth {color: blue; text-align: center;}.. currentday {border: 1px solid hitam, warna: # 00FF00; text-align: center;} </ style> <script language="JavaScript"> fungsi kalender () {var sekarang = new Date (); var bulan = current.getMonth (); var hari = current.getDate (); var tahun = current.getFullYear (); tempMonth = bulan +1; var tempDate = baru Tanggal (tempMonth + '1,' + tahun); var tempweekday = tempDate.getDay (); var tempweekday2 = tempweekday, jika (bulan == 1) {if ((tahun% 100 = 0) & & (tahun% 4 =! = 0) | | (tahun% 400 == 0)) {totalFeb = 29;} else {totalFeb = 28;}} var totalDays = ["31", "" + totalFeb +""," 31 "," 30 " , "31", "30", "31", "31", "30", "31", "30", "31"]; var bantalan = ""; sementara (tempweekday> 0) bantalan + = " <td class='premonth'> </ td> "; tempweekday -;} dayAmount = totalDays [bulan]; i =" 0 ", sedangkan (i <= dayAmount) {if (tempweekday2> 6) {tempweekday2 = 0 ; bantalan + = "</ tr> <tr>";} if (i == hari) {padding + = "<td class='currentday'>" + i + "</ td>";} else {padding + = "<td class='currentmonth'>" + i + "</ td>";} tempweekday2 + +; i + +;} var monthNames = ["Jan", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember"]; var calendarTable = "<table class='calendar'> <tr class='currentmonth'> < colspan = '7 th '> "+ monthNames [bulan] +" "+ tahun +" </ th> </ tr> "; calendarTable + =" <tr class='weekdays'> <td> Sun </ td> <td> Sen </ td> <td> Sel </ td> <td> Rabu </ td> <td> Kam </ td> <td> Jum </ td> <td> Sab </ td> </ tr> "; calendarTable + =" tr ";} </ script></span>
You can see an example of this code here . Anda dapat melihat contoh kode ini di sini . The example code has additional JavaScript applied to it that was from other tutorials on Code-Sucks. Contoh kode JavaScript tambahan telah diterapkan untuk itu yang dari tutorial lain di Kode-Sucks. Also, I have tested this code works in Firefox, and IE for Windows. Juga, saya telah diuji kode ini bekerja di Firefox, dan IE untuk Windows. It also works for Firefox on Linux, but Opera 9.50 Beta 1 (the 64-bit edition) has issues getting the correct day the month starts on. Ia juga bekerja untuk Firefox di Linux, tapi Opera 9.50 Beta 1 (edisi 64-bit) memiliki masalah mendapatkan hari yang benar dimulai pada bulan.
Tidak ada komentar:
Posting Komentar