Search code examples
actionscript-3airanimate-cc

Actionscripts 3 Function doesn't work


I write some code for getting current date and compare it with a future date for application limitation. I don't know why this function doesn't work.

getYYMMDD();

function getYYMMDD(): String {
    var dateObj: Date = new Date();
    var year: String = String(dateObj.getFullYear());
    var month: String = String(dateObj.getMonth() + 1);
    if (month.length == 1) {
        month = "0" + month;
    }
    var date: String = String(dateObj.getDate());
    if (date.length == 1) {
        date = "0" + date;
    }
    return year.substring(0, 4) + month + date;

    trace(year + ":" + month + ":" + date);
    if (int(year) > 2017 && int(month) > 5 && int(date) > 31) {
        trace("SYSTEM TIME IS OFF.");
    } else {
        trace("SYSTEM TIME IS ON.");
    }
}

Solution

  • (1) Since your function returns data of String type...

    function getYYMMDD(): String
    

    Make sure that returned data is also being received by a String... ie: someString = getYYMMDD(); means someString now has returned value from function.

    (2) You return (exit the function) too soon...

    Put return as last command to allow all other code inside your function to run.

    (3) You should consider returning a Boolean type (true/false)...

    var can_Start : Boolean = false; //# assume false before checking
    
    can_Start = getYYMMDD(); //# use function to update status to true/false
    
    if (can_Start == true) { run_Program(); }
    else { trace("Sorry time has expired"); }
    
    function getYYMMDD(): Boolean 
    {
        var dateObj: Date = new Date();
        var year: String = String(dateObj.getFullYear());
    
        var month: String = String(dateObj.getMonth() + 1);
        if (month.length == 1) { month = "0" + month; }
    
        var date: String = String(dateObj.getDate());
        if (date.length == 1) { date = "0" + date; }
    
        trace(year + ":" + month + ":" + date);
    
        if(int(year) == 2017)
        {   
            if(int(month) >= 05 && int(date) > 31)
            { trace("SYSTEM TIME IS OFF."); can_Start = false; } //# can_Start == false;
            else { trace("SYSTEM TIME IS ON."); can_Start = true; } //# can_Start == true;
        }
    
        return can_Start;
    }