• League Of Reason Forums will shut down 10th September 2025.
    There is a thread regarding this in General Discussion.

Javascript question

Laurens

Active Member
I'm totally struggling with a problem with javascript.

What I am aiming to do is to get today's date in Unix time like this:
Code:
<i>
</i>var today = new Date();
var date = today.getTime();

Then I want to take that date and run it through a function with 'if, else if' statements. Essentially my aim is to take 'date' and see where in falls in terms of a schedule in which closing times change depending on what week it is. For example:
Code:
if (date >1459728000 && date <1460332800){
	return '17.00';
}
else if (date >1460332800 && date <1460937600){
	return '17.15';
(that isn't the whole thing btw)

I cannot for the life of me get this to work. I'm a complete n00b when it comes to javascript, so if you understand what I'm on about, and you can see what I'm doing wrong could you please help me??

My basic aim is to take the date, ask if it is between date x and date y, and if it doesn't to go on to the next line and ask the same (where x and y are different values). It's totally frying my little brain...
 

Laurens

Active Member
May have worked out my error, I was working forwards in time, towards the future, whereas if I start 'if' with the furthest date in the future and work backwards, it may work....
 

Laurens

Active Member
Also realised that the date I was returning was wrong. Now I've divided it by 1000 to get the milliseconds it works fine.

Thanks for reading the most pointless thread ever made.

Sent from my SM-G920F using Tapatalk
 

Dragan Glas

Well-Known Member
Greetings,

One point I noted from your first post, if the date is exactly 1460332800, then the "if-else" clauses will fail.

You need to have either <= in the first line or >= in the second clause.

In other words:
if (date >1459728000 && date <=1460332800){
return '17.00';
}
else if (date >1460332800 && date <1460937600){
return '17.15';
OR
if (date >1459728000 && date <1460332800){
return '17.00';
}
else if (date >=1460332800 && date <1460937600){
return '17.15';
Whichever way you're trying to categorize the date/time.

Kindest regards,

James
 
Top