I am making an extension for me and people at my school which, to not get into too much detail, attempts to save your courses and tells you your upcoming schedule for months in advance. This is all possible because my school runs on a "Day" system. This means that on 'Day One' you might have math, science, english and gym, and on 'Day Two' you might have history, spanish, etc... you get the point. Anyway, for this extension to run, I need to keep track of the given day that I am on. This is only possible if someone opens the extension, which runs the script to check if a day has passed, and if so, will increment the variable. However, my problem is that some people might not open the extension for many days in a row, causing my program to lose what day it is, making it useless. So I need a way for my program to increment the days in the background without having to open the extension. Another way for me to do this is to somehow figure out how many days the user has been gone, and do some math to find out what day it is supposed to be.
If anyone can lend me some help that would be greatly appreciated. I have linked some of my code below.
This happens when the user first logs into my program, saving the day of the week they are on.
var today = new Date()
localStorage.setItem('day-of-week', today.getDay())
This 'checker' then gets run every time the extension is loaded, and if it notices any difference between the locally stored day of the week and the one right now, it will increment the school 'Day' and then store the new day of the week. (My school's day system only goes up to 4 so it wraps around back to one once it reaches 4)
function checkDate(prevDate) {
var today = new Date()
if (today.getDay() != prevDate) {
day = parseInt(day)
if (day == 4) {
day = 1
} else {
day = 1
}
localStorage.setItem('day', day)
localStorage.setItem('day-of-week', today.getDay())
}}
CodePudding user response:
I think a different approach the problem might solve it for you. If you know when the first day of the semester (or whatever date serves as the reference point for your schedule), you could just compare today's date to that date instead. A function for that might look something like:
const scheduleLength = 4
const milsPerDay = 1000 * 60 * 60 * 24
const semesterStart = new Date('September 3, 2022 00:00:00');
function scheduleDay(day){
var semesterDay = Math.floor( (day - semesterStart )/milsPerDay );
return semesterDay % scheduleLength
}
This function does not account for weekends since I wasn't sure if you need that or not, but you could definitely adjust this function to account for them if it's necessary. If you did need to do that, I'd personally recommend just renaming the semesterStart
variable and referencing the beginning of the week instead.
Just to make sure it worked, I wrote a simple loop:
for ( let i = 3; i <= 11; i ) {
var test = new Date('September ' i ', 2022 08:00:00')
console.log( scheduleDay(test), test)
}
and I got the output
> 0 Sat Sep 03 2022 08:00:00 GMT-0400 (Eastern Daylight Time)
> 1 Sun Sep 04 2022 08:00:00 GMT-0400 (Eastern Daylight Time)
> 2 Mon Sep 05 2022 08:00:00 GMT-0400 (Eastern Daylight Time)
> 3 Tue Sep 06 2022 08:00:00 GMT-0400 (Eastern Daylight Time)
> 0 Wed Sep 07 2022 08:00:00 GMT-0400 (Eastern Daylight Time)
> 1 Thu Sep 08 2022 08:00:00 GMT-0400 (Eastern Daylight Time)
> 2 Fri Sep 09 2022 08:00:00 GMT-0400 (Eastern Daylight Time)
> 3 Sat Sep 10 2022 08:00:00 GMT-0400 (Eastern Daylight Time)
> 0 Sun Sep 11 2022 08:00:00 GMT-0400 (Eastern Daylight Time)