Actionscript 3 - 解析yyyy-mm-dd hh:mm:ss到Date对象的最快方法?

时间:2008-08-06 08:33:53

标签: flex actionscript-3

我一直试图找到一种非常快速的方法来将yyyy-mm-dd [hh:mm:ss]解析为Date对象。以下是我尝试过的3种方法以及每种方法解析50,000个日期时间字符串所需的时间。

有没有人知道更快的方法或提示加快方法?

castMethod1 takes 3673 ms 
castMethod2 takes 3812 ms 
castMethod3 takes 3931 ms

代码:

private function castMethod1(dateString:String):Date {
    if ( dateString == null ) {
        return null;
    }

    var year:int = int(dateString.substr(0,4));
    var month:int = int(dateString.substr(5,2))-1;
    var day:int = int(dateString.substr(8,2));

    if ( year == 0 && month == 0 && day == 0 ) {
        return null;
    }

    if ( dateString.length == 10 ) {
        return new Date(year, month, day);
    }

    var hour:int = int(dateString.substr(11,2));
    var minute:int = int(dateString.substr(14,2));
    var second:int = int(dateString.substr(17,2));

    return new Date(year, month, day, hour, minute, second);
}

-

private function castMethod2(dateString:String):Date {
    if ( dateString == null ) {
        return null;
    }

    if ( dateString.indexOf("0000-00-00") != -1 ) {
        return null;
    }

    dateString = dateString.split("-").join("/");

    return new Date(Date.parse( dateString ));
}

-

private function castMethod3(dateString:String):Date {
    if ( dateString == null ) {
        return null;
    }

    var mainParts:Array = dateString.split(" ");
    var dateParts:Array = mainParts[0].split("-");

    if ( Number(dateParts[0])+Number(dateParts[1])+Number(dateParts[2]) == 0 ) {
        return null;
    }

    return new Date( Date.parse( dateParts.join("/")+(mainParts[1]?" "+mainParts[1]:" ") ) );
}

不,默认情况下,Date.parse不会处理破折号。我需要为"0000-00-00"等日期时间字符串返回null。

7 个答案:

答案 0 :(得分:17)

我一直在使用以下snipplet来解析UTC日期字符串:

private function parseUTCDate( str : String ) : Date {
    var matches : Array = str.match(/(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)Z/);

    var d : Date = new Date();

    d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));
    d.setUTCHours(int(matches[4]), int(matches[5]), int(matches[6]), 0);

    return d;
}

只需删除时间部分,它应该可以满足您的需求:

private function parseDate( str : String ) : Date {
    var matches : Array = str.match(/(\d\d\d\d)-(\d\d)-(\d\d)/);

    var d : Date = new Date();

    d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));

    return d;
}

不知道速度,我在我的应用程序中并没有担心。在我的机器上显着不到一秒的50K次迭代。

答案 1 :(得分:5)

这是一些摆弄后我能想到的最快的:

private function castMethod4(dateString:String):Date {          
    if ( dateString == null ) 
        return null;    
    if ( dateString.length != 10 && dateString.length != 19) 
        return null;

    dateString = dateString.replace("-", "/");
    dateString = dateString.replace("-", "/");

    return new Date(Date.parse( dateString ));
}

我在计算机上为castMethod2()获得了大约470ms的50k迭代,对于我的版本获得了300毫秒(这与63%的时间内完成的工作量相同)。我肯定会说两者都“足够好”,除非你解析愚蠢的日期。

答案 2 :(得分:2)

我猜测Date.Parse()不起作用?

答案 3 :(得分:1)

那么方法2似乎是最好的方法:

private function castMethod2(dateString:String):Date {
    if ( dateString == null ) {
        return null;
    }

    if ( dateString.indexOf("0000-00-00") != -1 ) {
        return null;
    }

    dateString = dateString.split("-").join("/");

    return new Date(Date.parse( dateString ));
}

答案 4 :(得分:1)

因为Date.parse()不接受所有可能的格式,我们可以使用DateFormatter和Data.parse()可以理解的formatString预先格式化传递的dateString值,例如

// English formatter
var stringValue = "2010.10.06"
var dateCommonFormatter : DateFormatter = new DateFormatter();
dateCommonFormatter.formatString = "YYYY/MM/DD";

var formattedStringValue : String = dateCommonFormatter.format(stringValue);                                
var dateFromString : Date = new Date(Date.parse(formattedStringValue));

答案 5 :(得分:1)

var strDate:String = "2013-01-24 01:02:40";

function dateParser(s:String):Date{
    var regexp:RegExp = /(\d{4})\-(\d{1,2})\-(\d{1,2}) (\d{2})\:(\d{2})\:(\d{2})/;
    var _result:Object = regexp.exec(s);

    return new Date(
        parseInt(_result[1]),
        parseInt(_result[2])-1,
        parseInt(_result[3]),
        parseInt(_result[4]),
        parseInt(_result[5]),
        parseInt(_result[6])
    );
}

var myDate:Date = dateParser(strDate);

答案 6 :(得分:0)

这是我的实施。试一试。

public static function dateToUtcTime(date:Date):String {
  var tmp:Array = new Array();
  var char:String;
  var output:String = '';

  // create format YYMMDDhhmmssZ
  // ensure 2 digits are used for each format entry, so 0x00 suffuxed at each byte

  tmp.push(date.secondsUTC);
  tmp.push(date.minutesUTC);
  tmp.push(date.hoursUTC);
  tmp.push(date.getUTCDate());
  tmp.push(date.getUTCMonth() + 1); // months 0-11
  tmp.push(date.getUTCFullYear() % 100);


  for(var i:int=0; i < 6/* 7 items pushed*/; ++i) {
    char = String(tmp.pop());
    trace("char: " + char);
    if(char.length < 2)
      output += "0";
    output += char;
  }

  output += 'Z';

  return output;
}