从MySQL UTC时间戳字符串创建的ActionScript Date对象字符串、对象、时间、UTC

2023-09-08 14:17:20 作者:夺掵書珄╄→

我存储在MySQL表的日期时间列日。我致电的 MySQL的UTC_CURRENTDATE 。当我找回它,它在下面的字符串格式为:2012-07-24 12:59:58

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 format: "2012-07-24 12:59:58"

当我尝试通过以下操作来创建动作脚本Date对象:

When I try to create a Date object in Action Script by doing the following:

var dateNum:Number = Date.parse(createDate); // this gives me NaN
var createDate:Date = new Date(dateNum);

Date.parse(2012-07-24 12:59:58) NaN的

解决方法:继杰森的意见,我做了以下内容:

Solution: Following Jason's advice, I'm doing the following:

选择一个 UNIX_TIMESTAMP (CREATE_DATE),返回我秒,因为1970-01-01 00:00:00UTC。然后做下面的动作脚本:

Select a UNIX_TIMESTAMP(CREATE_DATE) which returns me seconds since '1970-01-01 00:00:00' UTC. Then do following in Actionscript:

var createDate:Date = new Date();
var offset:Number = createDate.getTimezoneOffset() * 60 * 1000; 
createDate.time = parseInt("1343174921") * 1000 - offset;

这给了我正确的日期。

This gives me the right date.

推荐答案

虽然我敢肯定有一个更好的方法;显然,你可以实现一个解析功能,如:

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]);
}

调用为:

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

而不是处理MySQL的 DATETIME ,你的SQL语句可以转换为时间戳其动作日期的构造函数会接受,因为毫秒时代。

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

框架如 CASA库有很好的最新实用功能。

Frameworks such as CASA Lib have nice date utility functions.