Search code examples
androidactionscript-3air

AS3 check if date belongs to the range?


I want to check for, if the given date belongs to two given dates. Dates are in string format like "2015-02-19"


Solution

  • If all your dates are formatted as YYYY-MM-DD then they are alphabetical, so you can actually compare the strings using normal comparison operators (<, >, ==, <=, >=), because when both operands are strings they are compared alphabetically.

    So you could do this:

    var from:String = "2015-02-02";
    var to:String = "2015-06-06";
    
    function check(date:String):Boolean {
        return date >= from && date <= to;
    }
    
    check("2015-05-05"); // true
    check("2015-01-01"); // false
    check("2015-07-05"); // false
    check("2014-05-05"); // false
    // etc