Flex String to Date

本文探讨了三种不同的日期字符串解析为Date对象的方法,并对比了每种方法解析5万条日期数据所需的时间。第一种方法通过手动拆分字符串并创建Date对象;第二种方法利用Date.parse将转换后的日期字符串直接转化为Date对象;第三种方法则结合了字符串分割和Date.parse方法。
摘要由CSDN通过智能技术生成

http://hi.baidu.com/lushaofeng21/blog/item/0aaf8ffba5785b1b6c22eb37.html

 

 

 

 写道
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 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]:" ") ) ); 
} 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值