mercredi 25 février 2015

Javascript date object day subtraction not working with comparison if statement

I can't get the "&& partyDate >= pastParty)" section of the comparison statement to work. I think the problem lies within the "pastParty" variable, but I'm not sure what to do. It should be that if the partyDate is only 30 days or less past the curDate the third "if" will trigger. So if the partyDate is (2015, 2, 14) and the curDate is (2015, 2, 15) the party is over by one day.



<body>
<p id="theDate"></p><br>
<p id="theParty"></p><br>
<p id="quote"></p>
</body>

<script>
var curDate = new Date();
var partyDate = new Date();
curDate.setFullYear(2015, 2, 15);
partyDate.setFullYear(2015, 2, 14);

document.getElementById("theDate").innerHTML = "The current date is " + curDate;
document.getElementById("theParty").innerHTML = "The party is " + partyDate;

function getQuote() {
var futureParty = curDate.setDate(curDate.getDate() + 30);
var pastParty = curDate.setDate(curDate.getDate() - 30);
var daysTill = (partyDate - curDate) / (1000 * 60 * 60 * 24);
var daysPast = (curDate - partyDate) / (1000 * 60 * 60 * 24);
daysTill = Math.round(daysTill);
daysPast = Math.round(daysPast);

if (partyDate <= futureParty && partyDate > curDate) {
document.getElementById("quote").innerHTML = "The party will be here in " + daysTill + " days, let's pre-party!";
} else if (partyDate > futureParty) {
document.getElementById("quote").innerHTML = "The party is more than " + daysTill + " days from now!";
} else if (partyDate < curDate && partyDate >= pastParty) {
document.getElementById("quote").innerHTML = "The party is over for the year, but you only missed it by " + daysPast + " days.";
} else if (partyDate < pastParty) {
document.getElementById("quote").innerHTML = "The party was more than " + daysPast + " days ago!";
}
}
getQuote();

Aucun commentaire:

Enregistrer un commentaire