Jump to content

select from arrays in condition


remkovdmijde

Recommended Posts

Forgive me my english, i'm dutch ;-)

 

I want the folowing: If its thursday before 9pm, than i want it to display friday (27 hours later). With friday before 9pm i want it to display monday (75 hours later) etc. And i wat it to say it with the date behind it. Like this: Friday 19-09-2014 I have the following code:

 

function myFunction() {
var d = new Date();
if (weekday[1] || weekday[2] || weekday[3] || weekday[4]) {
d.setHours(d.getHours() + 27); //ma,di,wo,do voor 21.00 besteld, morgen binnen (ma->di,di->wo etc.)
} else if (weekday[0]) {
d.setHours(d.getHours() + 51); //zo besteld, 2 dagen later binnen (zo->di)
} else {
d.setHours(d.getHours() + 75); //vr,za besteld, 3 dagen later binnen (vr->ma, za->di)
}
var weekday = new Array(7);
weekday[0]= "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
document.getElementById("demo").innerHTML = n;
}
myFunction()
Link to comment
Share on other sites

what's your question?

 

It seems like something are out of order, you are checking in the weekdays array before you've defined anything. I'm not sure what the second case your testing for is, as you only specified two conditions in your post (and they seem to be the first and third cases in your if / else block).

 

Essentially though, it seems like you want to test for two things per case:

  1. Check the day (is it Thursday or Friday)
  2. Check the time (before 9pm)

Link to comment
Share on other sites

The array needs to be declared first before you can use it.

 

This won't work:

alert(a[0]);var a = new Array();a[0] = "A";a[1] = "B";

This will work:

var a = new Array();a[0] = "A";a[1] = "B";alert(a[0]);

The second thing you need to know is how to use the Date object. You can read about it in the W3Schoole tutorial: http://www.w3schools.com/js/js_date_methods.asp

Weekdays are numbered from 0 to 6 when returned by the Date() object. The name of the weekday is not used.

 

For the conditions, what you have to do is compare the values given by the date object to the values you want them to be compared to.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...