any idea how could I make what for 6 days a system which knows the date can make me to display some info depending on what is the actual date. EX: Day 1 : Info Day2: Info 2, but I do not know how can I make what the system knew what today is 5 and tomorrow will be 6 and then will be 1 and like this way again to 6. Not counting sunday and saturday.
The thing is what today is day 5 but tomorrow will be day 6. Example: It depends on the day if it will be day 1,2,3,4,5 or 6 the Monday can be day 1,2,3,4,5 or 6 depending. I want a way to count the day with the date. Today Feb 18 is day 5 so which day will be EX: 26 of Feb?.
For now I have this in my code but I'm stuck:
//Definiendo Fecha
var hoy = new Date();
var dd = hoy.getDate();
var mm = hoy.getMonth() + 1; //January is 0!
var yyyy = hoy.getFullYear();
if (dd < 10) {
dd = "0" + dd;
}
if (mm < 10) {
mm = "0" + mm;
}
hoy = dd + "/" + mm + "/" + yyyy;
//Fin
document.getElementById("fecha").innerHTML = hoy;
contador = 4
function obtenerDia() {
if (!(hoy = "17/02/2019")) {
contador++
}else {
console.log("Algo pasa.")
}
}
I will really appreciate your help.
Context: I'm building an schedule, so it should display the correct schedule of the day when it is EX: The day 1 ... 5 , depending on which day is the correct one.
For a generalized solution, consider:
function getDayNumber(baseDate, targetDate, daysInCycle) {
var startOfBaseDate = new Date(baseDate).setHours(0, 0, 0, 0);
var startOfTargetDate = new Date(targetDate).setHours(0, 0, 0, 0);
var differenceInDays = Math.round((startOfTargetDate - startOfBaseDate) / 864e5);
return ((differenceInDays % daysInCycle + daysInCycle) % daysInCycle) + 1;
}
baseDate
is any Date
object (or timestamp) that corresponds to "Day 1" of the cycle.targetDate
is the Date
object (or timestamp) you wish to find the day number for.daysInCycle
is the number of days before the cycle repeats, such as 6
for a six day cycle.Examples:
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 15), 6) //=> 4
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 16), 6) //=> 5
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 17), 6) //=> 6
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 18), 6) //=> 1
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 19), 6) //=> 2
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 20), 6) //=> 3
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 21), 6) //=> 4
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 22), 6) //=> 5
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 23), 6) //=> 6
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 24), 6) //=> 1
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 25), 6) //=> 2
getDayNumber(new Date(2019, 1, 18), new Date(2019, 1, 26), 6) //=> 3
For baseDate
or targetDate
, you could pass Date.UTC(2019, 1, 18)
if you wanted to use the UTC day as a basis.
For targetDate
, you can pass either new Date()
or Date.now()
to use the current timestamp.
Note also that the last line of the function uses the modulo solution given here in order to ensure that negative numbers (dates before the base date) are handled correctly.