Files
Backend-Api/ServiceHost/wwwroot/AssetsClient/js/7days.js
2024-07-05 21:36:15 +03:30

46 lines
2.0 KiB
JavaScript

const date = new Date;
const month = date.getMonth() + 1;
const day = date.getDay();
const dayNumber = date.getDate();
const weekDays = ['1 شنبه', '2 شنبه', '3 شنبه', '4 شنبه', '5 شنبه', 'آدینه', 'شنبه'];
const monthNames = ["January","February","March","April","May","June","July", "August","September","October","November","December"];
var weekDisplay = document.querySelector(".weekDisplay");
var wDisplayDay = document.querySelectorAll(".wDisplayDay");
var dayNumY = document.querySelectorAll(".dayNumY");
var dayNameX = document.querySelectorAll(".dayNameX");
function numOfDays(){
// this function returns the number of days between today and the 1st of next month
const oneDay = 24*60*60*1000 //hours*minutes*seconds*millseconds
let start = new Date(date.getFullYear(), month - 1, dayNumber);
let end = new Date(date.getFullYear(), month, 1)
const diffDays = Math.round(Math.abs((start - end) / oneDay));
return diffDays
}
function addDates(){
var currentDays = numOfDays() + dayNumber;
console.log(currentDays); // should alway return the number of days in the current month
for(var i = 0; i < currentDays; i++){
console.log(dayNumY[i].textContent);
dayNumY[i].textContent = dayNumber + Number([i]);
}
}
function addNames(){
if(day > 0){
let newArr = weekDays.slice(0, day); // slices from "day 0 (sunday)", up to today - saved in newArr
let newArr2 = weekDays.slice(day); // slices from today to end of weekDays array - saved in newArr2
let fixedArr = newArr2.concat(newArr); // by adding newArr (sunday - today) to the end of newArr2 (today - saturday)
// a new array is created using today as position zero.
for(var i = 0; i < fixedArr.length; i++){
dayNameX[i].textContent = fixedArr[i];
}
} else {
for(var i = 0; i < weekDays.length; i++){
dayNameX[i].textContent = weekDays[i];
}
}
}
addNames();
addDates();