I need to code a function that returns the date (example: 2014/08/28) of next tuesday or next friday, whichever is the closest day. But there is a little quibble: if today's day is tuesday or friday, if it is before 22:00 it should return todays date, on the other hand if it's after 22:00 or equal to 22:00 it should return next tuesday's or friday's date, whichever it's the next one.
I've no clue on how to start coding this.
Regards,
I need to code a function that returns the date (example: 2014/08/28) of next tuesday or next friday, whichever is the closest day. But there is a little quibble: if today's day is tuesday or friday, if it is before 22:00 it should return todays date, on the other hand if it's after 22:00 or equal to 22:00 it should return next tuesday's or friday's date, whichever it's the next one.
I've no clue on how to start coding this.
Regards,
Share Improve this question asked Aug 25, 2014 at 18:53 EgidiEgidi 1,7769 gold badges45 silver badges70 bronze badges 4- Start out by writing the steps of what is needed. It should be on paper and without using code. Then move onto the steps you would do to reach the requirements. Example: Step 1 find today's date. Step 2: pare to requirements.. etc Once you have listed out the steps from start to end (algorithm) you can then start on figuring out the code you need to perform these tasks. – scrappedcola Commented Aug 25, 2014 at 18:56
- 1 Yep i know the logic, it should be: 1. get todays date 2. check if it's tuesday or friday 3. if it is, check if it's before 22:00, if it is, return todays date, else return next tuesday/friday's..the only thing is that i don't know how to code it – Egidi Commented Aug 25, 2014 at 19:09
- 1 Sounds like an algorithm :) What is your programming level? Did you check developer.mozilla/en-US/docs/Web/JavaScript/Reference/… ? – aymericbeaumet Commented Aug 25, 2014 at 19:24
- Then you should check out the date object page developer.mozilla/en-US/docs/Web/JavaScript/Reference/…. If there are any parts of your algorithm that you can't figure out how to implement google a simple search string "Javascript get todays date", "javascript pare dates". And you should be set. IF you run into a specific problem beyond that e back with your code and clearly stated problem. – scrappedcola Commented Aug 25, 2014 at 19:38
1 Answer
Reset to default 7Check the sninppet below. Maybe I could make it more optimised but more or less, I just followed the pseudo-code from the ments above.
function closest_tuesday_or_friday() {
var today = new Date(), tuesday, friday, day, closest;
if(today.getDay() == 2 || today.getDay() == 5){
if(today.getHours() < 22){
return today.getFullYear() + "/" + (today.getMonth() + 1) + "/" + today.getDate();
}
}else{
day = today.getDay();
tuesday = today.getDate() - day + (day === 0 ? -6 : 2);
friday = today.getDate() - day + (day === 0 ? -6 : 5);
}
if(tuesday < friday){
closest = new Date(today.setDate(tuesday));
}else{
closest = new Date(today.setDate(friday));
}
return closest.getFullYear() + "/" + (closest.getMonth() + 1) + "/" + closest.getDate();
}
console.log(closest_tuesday_or_friday());
Working jsBin