Skip to content

Commit

Permalink
Merge pull request #8 from teod0036/master
Browse files Browse the repository at this point in the history
added formatTime to util.js, changed various timers to use formatTime…
  • Loading branch information
alexop1000 authored Mar 24, 2022
2 parents 354aaeb + f65be60 commit 1ae0b8c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 59 deletions.
25 changes: 1 addition & 24 deletions js/alle.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,7 @@ pages.alle = (async () => {
headerNav.innerHTML += `<p class="fricount">Du har fri om <strong id="fritid"></strong> <p>`;
setInterval(() => {
const timeUntilDate = response - (new Date())
let htmlToApply = "";
//shows "timer" as "time" if its value is one
if (Math.floor(timeUntilDate / (1000 * 60 * 60) % 24) == 1) {
htmlToApply += `${Math.floor(timeUntilDate / (1000 * 60 * 60) % 24)} time, `;
} else if (Math.floor(timeUntilDate / (1000 * 60 * 60) % 24) > 0) {
htmlToApply += `${Math.floor(timeUntilDate / (1000 * 60 * 60) % 24)} timer, `;
}

//shows "minutter" as "minut" if its value is one
if (Math.floor(timeUntilDate / (1000 * 60) % 60) == 1) {
htmlToApply += `${Math.floor(timeUntilDate / (1000 * 60) % 60)} minut og `;
} else if (Math.floor(timeUntilDate / (1000 * 60) % 60) > 0) {
htmlToApply += `${Math.floor(timeUntilDate / (1000 * 60) % 60)} minutter og `;
}


//shows "sekunder" as "sekund" if its value is one
if (Math.floor(timeUntilDate / 1000 % 60) == 1) {
htmlToApply += `${Math.floor(timeUntilDate / 1000 % 60)} sekund`;
} else {
htmlToApply += `${Math.floor(timeUntilDate / 1000 % 60)} sekunder`;
}


let htmlToApply = formatTime(timeUntilDate);
document.getElementById("fritid").innerText = htmlToApply;
}, 1000)
}
Expand Down
27 changes: 0 additions & 27 deletions js/ledige.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
const formatTime = (time) => {
const hours = Math.floor(time / (1000 * 60 * 60));
const minutes = Math.floor(time / (1000 * 60) % 60);
const seconds = Math.floor(time / 1000 % 60);
let string = "";
//shows "timer" as "time" if its value is one
if (hours == 1) {
string += `${hours} time, `;
} else if (hours > 0) {
string += `${hours} timer, `;
}

//shows "minutter" as "minut" if its value is one
if (minutes == 1) {
string += `${minutes} minut og `;
} else if (minutes > 0) {
string += `${minutes} minutter og `;
}

//shows "sekunder" as "sekund" if its value is one
if (seconds == 1) {
string += `${seconds} sekund`;
} else {
string += `${seconds} sekunder`;
}
return string;
}
pages.ledige = (async () => {
const skoleTal = window.location.href?.split("/")?.[4]
if (!skoleTal) return;
Expand Down
8 changes: 1 addition & 7 deletions js/opgaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,7 @@ pages.opgaver = (async () => {
const timeUntilDate = new Date(date).getTime() - new Date().getTime();

if (timeUntilDate > 0) {
let htmlToApply = `\n${Math.floor(timeUntilDate / (1000 * 60 * 60 * 24))} dage, ${Math.floor(timeUntilDate / (1000 * 60 * 60) % 24)} timer og ${Math.floor(timeUntilDate / (1000 * 60) % 60)} minutter`;
if (timeUntilDate < 1000 * 60 * 60 * 24) {
htmlToApply = `\n${Math.floor(timeUntilDate / (1000 * 60 * 60))} timer, ${Math.floor(timeUntilDate / (1000 * 60) % 60)} minutter og ${Math.floor(timeUntilDate / 1000 % 60)} sekunder`;
}
if (timeUntilDate < 1000 * 60 * 60) {
htmlToApply = `\n${Math.floor(timeUntilDate / (1000 * 60) % 60)} minutter og ${Math.floor(timeUntilDate / 1000 % 60)} sekunder`;
}
let htmlToApply = formatTime(timeUntilDate);
// Make it red if the time until date is less than 1 day
if (timeUntilDate < 1000 * 60 * 60 * 24) {
htmlToApply = `<span style="color: red">${htmlToApply}</span>`;
Expand Down
38 changes: 37 additions & 1 deletion js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,40 @@ const setSetting = async (setting, value) => {
setStorage("settings", storageSettings)
resolve(true)
})
}
}
//for formatting timers like fricounter, or "ledig"-counter
const formatTime = (time) => {
const days = Math.floor(time / (1000 * 60 * 60 * 24));
const hours = Math.floor(time / (1000 * 60 * 60) % 24);
const minutes = Math.floor(time / (1000 * 60) % 60);
const seconds = Math.floor(time / 1000 % 60);
let string = "";
//shows "dage" as "dag" if its value is one
if (days == 1) {
string += `${days} dag, `;
} else if (days > 0) {
string += `${days} dage, `;
}

//shows "timer" as "time" if its value is one
if (hours == 1) {
string += `${hours} time, `;
} else if (hours > 0) {
string += `${hours} timer, `;
}

//shows "minutter" as "minut" if its value is one
if (minutes == 1) {
string += `${minutes} minut og `;
} else if (minutes > 0) {
string += `${minutes} minutter og `;
}

//shows "sekunder" as "sekund" if its value is one
if (seconds == 1) {
string += `${seconds} sekund`;
} else {
string += `${seconds} sekunder`;
}
return string;
}

0 comments on commit 1ae0b8c

Please sign in to comment.