Reputation: 397
1.Example: condition is valid
date_from = "2014-05-21 16:00:00";
date_now = "2014-05-21 18:00:00";
date_to = "2014-05-21 19:00:00";
2.Example: condition is valid
date_from = "2014-05-21 16:00:00";
date_now = "2014-05-21 18:00:00";
date_to = null;
3.Example: condition is valid
date_from = null;
date_now = "2014-05-21 18:00:00";
date_to = "2014-05-21 19:00:00";
4.Example: condition is not valid
date_from = "2014-05-21 16:00:00";
date_now = "2014-05-21 18:00:00";
date_to = "2014-05-21 17:00:00";
I have this condition:
if (data1.date_from <= voting_started && voting_started <= data1.date_to) {
//abc
}
It works well, if both date_from and date_to are not null, but if one is null, condition is not valid. How to check date with null value?
Upvotes: 0
Views: 61
Reputation: 32831
or:
if ((data1.date_from === null || data1.date_from <= voting_started) &&
(data1.date_to === null || voting_started <= data1.date_to)) {
//abc
}
Upvotes: 1
Reputation: 812
Normalize it.
Before your if
add this:
if(!date_to) {
date_to = voting_started;
};
if(!date_from) {
date_from = voting_started;
}
Upvotes: 2