Reputation: 395
Hi I have the following if then statement in my JS code.
if (attributes.DATE > '10/20/2013'){
html += '<b>Maximum percentage:</b> ' + aPerc + '%';
}
else {
html += '<b>Percentage:</b> ' + aPerc + '%';
}
It seems to be working fine for October 2013 dates, and all January dates (no matter the year).
For past October years, it will display "Maximum percentage:" for Oct. 1-19 and "Percentage" for Oct. 20-31st.
It does not work correctly for other months. E.g. Feb, March, April, etc. it displays "Maximum Percentage".
I am guessing it has something to do with my date format? Thank you.
Upvotes: 0
Views: 1215
Reputation: 91726
I would do this:
var date1 = new Date(attributes.DATE);
var date2 = new Date('10/20/2013');
if (date1 > date2){
html += '<b>Maximum percentage:</b> ' + aPerc + '%';
}
else {
html += '<b>Percentage:</b> ' + aPerc + '%';
}
That way, you're comparing two date objects rather than two string objects.
Upvotes: 4
Reputation: 1075735
You're doing a string comparison, but the order of the fields in the string will not give you a date-order comparison. (You'd get one if both strings were in yyyy/MM/dd
order...)
If DATE
is a Date
instance, you can do this:
if (attributes.DATE >= new Date(2013, 9, 21)){
...which will be true from midnight on October 21st (the month value starts with 0 = January, so 9 = October). Note I made it >=
and the 21st rather than the 20th, because I assumed if you're looking for something > '10/20/2013'
, you mean the 21st or later (as opposed to meaning "any time after midnight on the 20th").
Upvotes: 1