Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. function calendar([day, month, year]) {
  2. [day, month, year] = [day, month, year].map(Number);
  3. var date = new Date(year, month - 1, day);
  4. let dates = getDateToExport(date);
  5. var html = '';
  6. html += `<table>\n`;
  7. let headers = 'Sun Mon Tue Wed Thu Fri Sat'.split(' ');
  8. html += `<tr>`;
  9. for (let h of headers) {
  10. html += `<th>${h}</th>`;
  11. }
  12. html += `</tr>\n`;
  13. for (let day of dates) {
  14. if (day.getDay() === 0) {
  15. html += '<tr>';
  16. }
  17. if (day.getMonth() < date.getMonth() && day.getYear() <= date.getYear()) {
  18. html += `<td class="prev-month">${day.getDate()}</td>`;
  19. } else if (day.getMonth() > date.getMonth() || day.getYear() > date.getYear()) {
  20. html += `<td class="next-month">${day.getDate()}</td>`;
  21. } else if (day.getDate() === date.getDate()) {
  22. html += `<td class="today">${day.getDate()}</td>`;
  23. }
  24. else {
  25. html += `<td>${day.getDate()}</td>`;
  26. }
  27.  
  28. if (day.getDay() == 6) {
  29. html += `</tr>\n`;
  30. }
  31.  
  32. }
  33. html += `</table>`;
  34.  
  35. console.log(html.trim());
  36. return ;
  37.  
  38.  
  39. function getDateToExport(date) {
  40. let dates = [];
  41. let firstDate = new Date(date.getFullYear(),date.getMonth(),0);
  42. firstDate.setDate(firstDate.getDate() - firstDate.getDay()%6)
  43.  
  44. let lastDate = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  45. lastDate.setDate(lastDate.getDate() + (6 - lastDate.getDay()));
  46. var timeDiff = Math.abs(lastDate.getTime() - firstDate.getTime());
  47. var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
  48. dates.push(new Date(firstDate));
  49. for (let i = 1; i <= diffDays; i++) {
  50.  
  51. dates.push(addDays(firstDate, i));
  52. }
  53. return dates;
  54.  
  55. }
  56.  
  57. function addDays(date, days) {
  58. let result = new Date(date);
  59. result.setDate(result.getDate() + days);
  60. return result;
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement