I am working on making a working calendar. I need my dates to align with my dates (as of now, it is Wednesday 27th, yet my calendar makes it appear as Monday because the first dates are misaligned).
I know that the issue is the misaligned dates. I am struggling with figuring out a solution that would not require manual input (I want it to be automated)
var date = new Date();
// gets new date, which is the current date.
function getMonthName(month) {
// create array to hold name of each month
var monthNames = new Array(12);
monthNames[0] = "January";
monthNames[1] = "February";
monthNames[2] = "March";
monthNames[3] = "April";
monthNames[4] = "May";
monthNames[5] = "June";
monthNames[6] = "July";
monthNames[7] = "August";
monthNames[8] = "September";
monthNames[9] = "October";
monthNames[10] = "November";
monthNames[11] = "December";
// return name of specified month (parameter)
return monthNames[month];
}
console.log(getMonthName(1)); // Februrary
var currentDay = date.getDay();
// gets the day within a week. (sunday - saturday, 0-6)
var totalDays = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
// -> (2022, 4 (May), 0 (last day of last month)).getDate(); -> 30
// console.log(totalDays);
let week0 = document.createElement("week0");
let tbody = document.querySelector("#dates");
tbody.appendChild(week0);
// for the for loop
for (day = 0; day < currentDay; day++) {
// for first row
// does 4 times for wed, 20/04/2022
let new_td = document.createElement("td");
let table_row = document.querySelector("#week0"); // selecting week0 - line 12
table_row.appendChild(new_td); //adding to cells to row
new_td.innerText = "";
}
// align the weekday to the date
// for (weekDay = 0; weekDay < currentDay; weekDay++) {
// }
// needs to stop at 5 for friday = 1st. april 2022.
for (day = 0; day < totalDays; day++) {
// for rest of the days
let new_td = document.createElement("td");
let table_row = document.querySelector(
"#week" + parseInt((day + currentDay) / 7)
);
console.log(table_row);
// "#week" + parseInt((1+3)/7), table_row = querySelector("#week + 5/7") -> week#0
// "#week" + parseInt((4+3)/7), table_row = querySelector("#week + 5/7") -> week#1
// parseInt() = string -> integer
table_row.appendChild(new_td);
// add
new_td.innerText = day + 1;
// changes the cell text
}
// simulate in mind
// add day view?
// https://www.youtube.com/watch?v=YL3ufzQ0QC4
// *create event using forms (for tracking locally)
// https://codepen.io/corvus-007/pen/MzEKyJ
@import url("https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;1,100;1,200;1,300;1,400;1,500&display=swap");
/* 'Roboto Mono', sans-serif; */
* {
text-align: center;
font-family: "Roboto Mono", sans-serif;
}
table {
margin-left: auto;
margin-right: auto;
width: 100%;
border-collapse: collapse;
}
table,
th,
td {
border: 1px solid;
padding: 10px 0 40px 0;
/* padding: 61px; */
/* text-align: right; */
}
th {
background-color: rgb(245, 143, 143);
color: rgb(0, 0, 0);
font-weight: bold;
}
td:hover {
background-color: rgb(178, 172, 187);
color: rgb(255, 255, 255);
}
.days_of_the_week th {
padding: 3rem;
text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<script src="script.js" defer></script>
<title>Habit Tracker</title>
</head>
<body>
<h1>HABIT TRACKER</h1>
<h2 id="month">Month</h2>
<table>
<thead id="days_of_week" class="days_of_week">
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</thead>
<tbody id="dates">
<tr id="week0"></tr>
<tr id="week1"></tr>
<tr id="week2"></tr>
<tr id="week3"></tr>
<tr id="week4"></tr>
</tbody>
</table>
</body>
</html>