Creating ActionScript Date object from MySQL UTC timestamp string

前端 未结 1 351
小鲜肉
小鲜肉 2021-01-22 21:45

I\'m storing Date in datetime column of MySQL table. I\'m inserting current date by calling MySql\'s UTC_CURRENTDATE. When I retrieve it, it\'s in following string

相关标签:
1条回答
  • 2021-01-22 22:03

    Although I'm sure there's a more elegant approach; clearly you could implement a parse function such as:

    public static function parse(date:String):Date
    {
        var split:Array = date.split(" ");
        var splitDate:Array = split[0].split("-");
        var splitTime:Array = split[1].split(":");
    
        return new Date(splitDate[0],
                        splitDate[1] - 1,
                        splitDate[2],
                        splitTime[0],
                        splitTime[1],
                        splitTime[2]);
    }
    

    Called as:

    var date:Date = parse("2012-07-24 12:59:58");
    

    Instead of handling MySQL DATETIME, your SQL statements could convert to timestamps which ActionScript Date constructor would accept milliseconds since epoch.

    Frameworks such as CASA Lib have nice date utility functions.

    0 讨论(0)
提交回复
热议问题