<p>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.</p>

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

<pre><code>castMethod1 takes 3673 ms 
castMethod2 takes 3812 ms 
castMethod3 takes 3931 ms
</code></pre>

<p>Code:</p>

<pre><code>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 &amp;&amp; month == 0 &amp;&amp; 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);
}
</code></pre>

<p>-</p>

<pre><code>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 ));
}
</code></pre>

<p>-</p>

<pre><code>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]:" ") ) );
}
</code></pre>

<hr>

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

[{'answer_id': 3185, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 2, 'selected': False, 'text': "<p>I'm guessing Date.Parse() doesn't work?</p>\n"}, {'answer_id': 3194, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 1, 'selected': False, 'text': '<p>Well then method 2 seems the best way:</p>\n\n<pre><code>private function castMethod2(dateString:String):Date {\n    if ( dateString == null ) {\n        return null;\n    }\n\n    if ( dateString.indexOf("0000-00-00") != -1 ) {\n        return null;\n    }\n\n    dateString = dateString.split("-").join("/");\n\n    return new Date(Date.parse( dateString ));\n}\n</code></pre>\n'}, {'answer_id': 9904, 'author': 'grapefrukt', 'author_id': 914, 'author_profile': 'https://Stackoverflow.com/users/914', 'pm_score': 3, 'selected': False, 'text': '<p>This was the fastest I could come up with after some fiddling:</p>\n\n<pre><code>private function castMethod4(dateString:String):Date {          \n    if ( dateString == null ) \n        return null;    \n    if ( dateString.length != 10 &amp;&amp; dateString.length != 19) \n        return null;\n\n    dateString = dateString.replace("-", "/");\n    dateString = dateString.replace("-", "/");\n\n    return new Date(Date.parse( dateString ));\n}\n</code></pre>\n\n<p>I get 50k iterations in about 470ms for castMethod2() on my computer and 300 ms for my version (that\'s the same amount of work done in 63% of the time). I\'d definitely say both are "Good enough" unless you\'re parsing silly amounts of dates.  </p>\n'}, {'answer_id': 10030, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 5, 'selected': True, 'text': "<p>I've been using the following snipplet to parse UTC date strings:</p>\n\n<pre><code>private function parseUTCDate( str : String ) : Date {\n    var matches : Array = str.match(/(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d) (\\d\\d):(\\d\\d):(\\d\\d)Z/);\n\n    var d : Date = new Date();\n\n    d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));\n    d.setUTCHours(int(matches[4]), int(matches[5]), int(matches[6]), 0);\n\n    return d;\n}\n</code></pre>\n\n<p>Just remove the time part and it should work fine for your needs:</p>\n\n<pre><code>private function parseDate( str : String ) : Date {\n    var matches : Array = str.match(/(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)/);\n\n    var d : Date = new Date();\n\n    d.setUTCFullYear(int(matches[1]), int(matches[2]) - 1, int(matches[3]));\n\n    return d;\n}\n</code></pre>\n\n<p><strike>No idea about the speed, I haven't been worried about that in my applications.</strike> 50K iterations in significantly less than a second on my machine.</p>\n"}, {'answer_id': 3016172, 'author': 'JabbyPanda', 'author_id': 193063, 'author_profile': 'https://Stackoverflow.com/users/193063', 'pm_score': 1, 'selected': False, 'text': '<p>Because Date.parse() does not accept all possible formats, we can preformat the passed dateString value using DateFormatter with formatString that Data.parse() can understand, e.g</p>\n\n<pre><code>// English formatter\nvar stringValue = "2010.10.06"\nvar dateCommonFormatter : DateFormatter = new DateFormatter();\ndateCommonFormatter.formatString = "YYYY/MM/DD";\n\nvar formattedStringValue : String = dateCommonFormatter.format(stringValue);                                \nvar dateFromString : Date = new Date(Date.parse(formattedStringValue));\n</code></pre>\n'}, {'answer_id': 6972550, 'author': 'Abhinav Mehta', 'author_id': 882693, 'author_profile': 'https://Stackoverflow.com/users/882693', 'pm_score': 0, 'selected': False, 'text': '<p>Here is my implementation. Give this a try.</p>\n\n<pre><code>public static function dateToUtcTime(date:Date):String {\n  var tmp:Array = new Array();\n  var char:String;\n  var output:String = \'\';\n\n  // create format YYMMDDhhmmssZ\n  // ensure 2 digits are used for each format entry, so 0x00 suffuxed at each byte\n\n  tmp.push(date.secondsUTC);\n  tmp.push(date.minutesUTC);\n  tmp.push(date.hoursUTC);\n  tmp.push(date.getUTCDate());\n  tmp.push(date.getUTCMonth() + 1); // months 0-11\n  tmp.push(date.getUTCFullYear() % 100);\n\n\n  for(var i:int=0; i &lt; 6/* 7 items pushed*/; ++i) {\n    char = String(tmp.pop());\n    trace("char: " + char);\n    if(char.length &lt; 2)\n      output += "0";\n    output += char;\n  }\n\n  output += \'Z\';\n\n  return output;\n}\n</code></pre>\n'}, {'answer_id': 20170012, 'author': 'Romeo', 'author_id': 3026302, 'author_profile': 'https://Stackoverflow.com/users/3026302', 'pm_score': 1, 'selected': False, 'text': '<pre><code>var strDate:String = "2013-01-24 01:02:40";\n\nfunction dateParser(s:String):Date{\n    var regexp:RegExp = /(\\d{4})\\-(\\d{1,2})\\-(\\d{1,2}) (\\d{2})\\:(\\d{2})\\:(\\d{2})/;\n    var _result:Object = regexp.exec(s);\n\n    return new Date(\n        parseInt(_result[1]),\n        parseInt(_result[2])-1,\n        parseInt(_result[3]),\n        parseInt(_result[4]),\n        parseInt(_result[5]),\n        parseInt(_result[6])\n    );\n}\n\nvar myDate:Date = dateParser(strDate);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/3163', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/22/']