I am delving into trying to write some JavaScript code. I understand the basics, (the very basics...) enough that I have been able to write the following and it works.
However, my knowledge doesn't stretch enough to be able to work out if this was the "best" way to write what I am trying to achieve. I don't want to just settle on it being "done" because it works, if there is a way to improve it and in turn, improve my understanding, I'd like to learn from people on best practices.
Currently, the code does the following:
If a user comes to the page before Sales Week, a message will display to the user telling them the days/hours/minutes until SalesWeek
(Please note that you won't be able to actually see the countdown at present on the code provided as it's a different piece of JS which is why it's in a funny span but it was written by a predecessor and I know it works.)
If a user comes to the page during sales week it will show which daily deal is open (and how long there is until it expires, using the same countdown timer as above which sadly is not included in this code but does work) and which deals have expired with a content div below for the actual sale items.
If the user comes to the page AFTER sales week they get a message to say that all the sales are over.
As a caveat, I know that using the client date is not ideal, and that server time should be used. Also I think this could have all been created in the back-end (where the deals couldn't be found out by someone looking at the source) but again, this isn't realistic for right now.
I'd really appreciate any guidance on my javascript itself, in the way I have formed it, and if there are better ways to achieve the same result.
Here is a demo on JSFiddle.
var date=new Date();
var year=date.getFullYear();
var month=date.getMonth();
var day=date.getDate();
var hours=date.getHours();
var minutes=date.getMinutes();
function SetDivContent() {
if ((year == 2015 && month < 10) || (year == 2015 && month == 10 && day < 23) || (year == 2015 && month == 10 && day == 23 && hours < 01)) {
console.log("We are PRE sale week");
document.getElementById("bem-sale-week-tocome").className = "show";
}
else if (year == 2015 && month == 10 && day >= 28) {
console.log("sales week has expired");
document.getElementById("bem-sale-week-ended").className = "show";
document.getElementById("day-one").className = "bem-sale-week--item-expired";
document.getElementById("day-two").className = "bem-sale-week--item-expired";
document.getElementById("day-three").className = "bem-sale-week--item-expired";
document.getElementById("day-four").className = "bem-sale-week--item-expired";
document.getElementById("day-five").className = "bem-sale-week--item-expired";
}
else {
console.log("We are IN Sales Week. sales Week is active");
document.getElementById("bem-sale-week-currentdeal").className = "show";
if ( (day=="23" && hours>="01") || (day=="24" && hours<="00") ) {
document.getElementById("day-one").className = "bem-sale-week--item-open";
document.getElementById("day-one-list").className += " open";
}
else if ( (day=="24" && hours>="01") || (day=="25" && hours<="00") ){
document.getElementById("day-one").className = "bem-sale-week--item-expired";
document.getElementById("day-two").className = "bem-sale-week--item-open";
document.getElementById("day-two-list").className += " open";
}
else if ( (day=="25" && hours>="01") || (day=="26" && hours<="00") ){
document.getElementById("day-one").className = "bem-sale-week--item-expired";
document.getElementById("day-two").className = "bem-sale-week--item-expired";
document.getElementById("day-three").className = "bem-sale-week--item-open";
document.getElementById("day-three-list").className += " open";
}
else if( (day=="26" && hours>="01") || (day=="27" && hours<="00") ){
document.getElementById("day-one").className = "bem-sale-week--item-expired";
document.getElementById("day-two").className = "bem-sale-week--item-expired";
document.getElementById("day-three").className = "bem-sale-week--item-expired";
document.getElementById("day-four").className = "bem-sale-week--item-open";
document.getElementById("day-four-list").className += " open";
}
else if( (day=="27" && hours>="01") || (day=="28" && hours<="00") ){
document.getElementById("day-one").className = "bem-sale-week--item-expired";
document.getElementById("day-two").className = "bem-sale-week--item-expired";
document.getElementById("day-three").className = "bem-sale-week--item-expired";
document.getElementById("day-four").className = "bem-sale-week--item-expired";
document.getElementById("day-five").className = "bem-sale-week--item-open";
document.getElementById("day-five-list").className += " open";
}
}
}
window.onload = SetDivContent;
if
statements. \$\endgroup\$