Skip to content

Commit

Permalink
Weeks between and current day
Browse files Browse the repository at this point in the history
  • Loading branch information
aunefyren committed Oct 12, 2023
1 parent b141eb5 commit decd822
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
5 changes: 5 additions & 0 deletions web/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
--lightgreen: #00A352;
--eggshell: #e0e1dd;
--lightblue: #778da9;
--trans-lightblue: #778da980;
--mediumblue: #415a77;
--darkblue: #0d1b2a;
--black: #181818;
Expand Down Expand Up @@ -1866,4 +1867,8 @@ input[type=checkbox]
overflow: hidden;
transition: 2s;
text-align: center;
}

.active-day {
background-color: var(--trans-lightblue);
}
19 changes: 9 additions & 10 deletions web/js/frontpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,14 @@ function place_week(week, fireworks) {
document.getElementById("day_7_date").value = week.days[6].date
document.getElementById("day_7_id").value = week.days[6].ID

// Place editing icon
// Find day int
const now = new Date(Date.now());
var day = now.getDay();

// Add class to current day
document.getElementById("day_" + day + "_check").classList.add("active-day")

// Place editing icon for exercise
if(day == 0) {
day = 7
}
Expand Down Expand Up @@ -1114,16 +1119,10 @@ function GoToExercise(exerciseID) {

function placeSeasonProgress(seasonStartObject, seasonEndObject) {

// Subtract 7 days
//seasonStartObject.setDate(seasonStartObject.getDate() - 7);
//seasonEndObject.setDate(seasonEndObject.getDate() - 7);

weekSum = weeksBetween(seasonStartObject, seasonEndObject)

now = new Date();
weekAmount = weeksBetween(seasonStartObject, now)
const weekSum = weeksBetween(seasonStartObject, seasonEndObject)

console.log("Current weeK: " + weekAmount)
const now = new Date();
const weekAmount = weeksBetween(seasonStartObject, now) - 1

document.getElementById("weeks_so_far").innerHTML = weekAmount
document.getElementById("weeks_total").innerHTML = weekSum
Expand Down
78 changes: 53 additions & 25 deletions web/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,30 +501,36 @@ function GetShortDate(dateTime) {
Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

dowOffset = typeof(dowOffset) == 'number' ? dowOffset : 0; //default dowOffset to zero
var newYear = new Date(this.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset; //the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() -
(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if(day < 4) {
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52) {
nYear = new Date(this.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weeknum = nday < 4 ? 1 : 53;
}
}
else {
weeknum = Math.floor((daynum+day-1)/7);
dowOffset = typeof(dowOffset) == 'number' ? dowOffset : 0; //default dowOffset to zero
var newYear = new Date(this.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset; //the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() -
(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if(day < 4) {
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52) {
nYear = new Date(this.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weeknum = nday < 4 ? 1 : 53;
}
}
else {
weeknum = Math.floor((daynum+day-1)/7);
}
return weeknum;
};
};

Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}

function HTMLDecode(text) {
var txt = document.createElement("textarea");
Expand Down Expand Up @@ -639,8 +645,30 @@ function setPrizeReceived(debt_id) {
return false;
}

function getWeekNumber(currentDate) {
const startDate = new Date(currentDate.getFullYear(), 0, 1);
const days = Math.floor((currentDate - startDate) / (24 * 60 * 60 * 1000));

return Math.ceil(days / 7);
}

function weeksBetween(dt2, dt1) {
var diff =(dt2.getTime() - dt1.getTime()) / 1000;
diff /= (60 * 60 * 24 * 7);
return Math.abs(Math.round(diff));

var startYear = dt2.getFullYear()
var startWeek = getWeekNumber(dt2)
const endYear = dt1.getFullYear()
const endWeek = getWeekNumber(dt1)

let weeksBetween = 1;

while(startYear < endYear || startWeek < endWeek) {
dt2 = dt2.addDays(7);

startYear = dt2.getFullYear()
startWeek = getWeekNumber(dt2)

weeksBetween += 1;
}

return weeksBetween;
}

0 comments on commit decd822

Please sign in to comment.