我从一个php文件接收到一个日期字段,我需要格式化它。
我得到这个字符串:“2009-11-12 17:58:13”
我想改成“2009年11月12日下午5:58:13”
我试着和约会班合作,但是我得到了这样的东西:
类型强制失败:无法将“2009-11-12 17:58:13”转换为日期。
有人知道做这个有什么好的工具吗?

最佳答案

如果没有直接的解决方案,可以使用regex:

private function formatDate(str:String):String
{

    var regex:RegExp = /(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/;
    var matches:Object = regex.exec(str);
    var date:Date = new Date(matches[1], Number(matches[2]) - 1, matches[3],
        matches[4], matches[5], matches[6]);
    var months:Array = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
        "Sep", "Oct", "Nov", "Dec"];
    var result:String = months[date.month] + " ";
    result += date.date + ", " + date.fullYear + " ";
    if(date.hours > 12)
        result += (date.hours - 12) + ":" + date.minutes + ":" +
            date.seconds + " pm";
    else
        result += date.hours + ":" + date.minutes + ":" + date.seconds + " am";
    return result;
}

regex的解释/(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/
// Forward slashes - / - at the beginning and end are delimiters

\d+ /* One or more number of digits.
     * Ideally it should be \d{n} (\d{4} - 4 digits),
     * but I'm assuming that the input string would always be
     * valid and properly formatted
     */

- and : //A literal hyphen/colon

\s   //A whitespace (use \s+ to skip extra whitespaces)

()  /* Parenthetic groups used to capture a string for using it later
     * The return value of regex.exec is an array whose first element is
     * the complete matched string and subsequent elements are contents
     * of the parenthetic groups in the order they appear in the regex
     */

关于php - as3-从mysql格式化时间戳字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1726586/

10-10 22:25