How to recognise date format?

时间:2016-04-07 10:47:31

标签: ios objective-c nsdate nsdateformatter

In my application I am working on a module of converting string to MMMM yyyy date format and insert it in database

Input:- NSString which may be of MMMM yyyy or dd/MM/yyyy hh:mm:ss format
Output:- NSString with MMMM yyyy format

And I Implement method like below

-(NSString *)convertToRequiredDataString:(NSString *)downloadedString
{
    NSString *stringToInsert=downloadedString;
    NSArray *checkArray=[stringToInsert componentsSeparatedByString:@"/"];
    if (checkArray.count>=2) {
        NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"dd/MM/yyyy 00:00:00"];
        NSDate *date=[formatter dateFromString:stringToInsert];
        [formatter setDateFormat:@"MMMM yyyy"];
        stringToInsert=[formatter stringFromDate:date];
        if (!stringToInsert) {
            stringToInsert=@"";
        }
    }
    return stringToInsert;
}

My question is that, Instead of checking the array length as checkArray.count>=2 is there any logically correct way of implementation possible

0 个答案:

没有答案