actionscript-3dateundefinedflashvars

Equivalent to "undefined" for Date type


I'm working on a video player and I have some troubles with flashvars. This is how I define my flashvars :

var beginLive:Date = getFlashVar('beginLive', "undefined");
var endLive:Date = getFlashVar('endLive', "undefined");

I have to retrieve the values (a timestamp) of those vars. My function is going to convert seconds to seconds/minutes/hours --> It is calculating the expected duration of a stream.

This is my getFlashVar function :

//return the content for the flashvar specified by varName, if found, otherwise return the defaultvalue specified

    function getFlashVar(varName:String, defaultValue:String):String {
        var result:String = defaultValue;
        if (undefined != paramsArray[varName] && '' != paramsArray[varName]) {
            result = paramsArray[varName] ;
            //ExternalInterface.call("displayVar", paramsArray);
        }
        return result;
    }

My problem is that I have to initialize beginLive and endLive to "undefined" but they are of type Date and "undefined" is a string.

Is there an equivalent to "undefined" for the Date type ? How can I initialize those vars and avoid Flash complaining about incompatible types ?

Thank you for your answers :).

Lea.


Solution

  • You could add a convenience method that tries to parse the resulting string from the getFlashVar function as a Date object. It returns a Date object if the input string could be parsed to a date and otherwise returns undefined.

    public static function parseDateFromString(value:String):Date {
        var dateObject:Number = Date.parse(value);
        if (isNaN(dateObject)) {
            return undefined;
        }
        return new Date(dateObject);
    }
    

    To use it, pass the returning value from getFlashVar to parseDateFromString like this:

    var beginLive:Date = parseDateFromString(getFlashVar('beginLive', "undefined"));
    var endLive:Date = parseDateFromString(getFlashVar('endLive', "undefined"));
    

    Example output:

    trace(parseDateFromString("01/30/2014")); // Thu Jan 30 00:00:00 GMT+0100 2014 (Date object)
    trace(parseDateFromString("undefined")); // null (actually undefined)