动作3 - 最快解析YYYY-MM-DD HH方式:MM:SS到Date对象?对象、最快、动作、方式

2023-09-08 12:33:24 作者:佛係少女

我一直在试图找到一个真正快速的方法来分析YYYY-MM-DD [HH:MM:SS]成Date对象。这里有3种方法我都试过这样做,它需要每一个方法的时间来分析50000日期时间字符串。

I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss] into a Date object. Here are the 3 ways I have tried doing it and the times it takes each method to parse 50,000 date time strings.

有谁知道这样做或提示,以加快方法中的任意更快的方法?

Does anyone know any faster ways of doing this or tips to speed up the methods?

castMethod1需要3673毫秒 castMethod2需要3812毫秒 castMethod3需要3931毫秒

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将不会默认处理破折号。我需要返回null日期时间字符串,如0000-00-00。

No, Date.parse will not handle dashes by default. And I need to return null for date time strings like "0000-00-00".

推荐答案

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

I've been using the following snipplet to parse UTC date strings:

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

只是删除​​了一部分时间,它应该能正常运行为您的需要:

Just remove the time part and it should work fine for your needs:

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迭代中显著不到一秒钟我的机器上。

No idea about the speed, I haven't been worried about that in my applications. 50K iterations in significantly less than a second on my machine.