Chengappa C D
Chengappa C D

Reputation: 1891

Getting date from string in objective C

I have to obtain date from a string, here the string will be in format "Monday 02,Dec 2013" i have to convert it to "2013-12-2" .I used the following code but I'm getting wrong output :

-(void)dateSelectedInPickerView:(NSString *)dateSelected{

    NSDateFormatter *dateFormatterForGettingDate = [[NSDateFormatter alloc] init];
    [dateFormatterForGettingDate setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    // Parse the string representation of the date i.e Monday 2,Dec 2013

    NSDate *dateFromStr = [dateFormatter dateFromString:dateSelected];
    NSLog(@"date selected  : %@",dateFromStr);

    NSDateFormatter *tempFormatter=[[NSDateFormatter alloc]init];
    [tempFormatter setDateFormat:@"YYYY-MM-dd"];

    self.reservationDateSelected=[tempFormatter stringFromDate:dateFromStr];
    NSLog(@"date Selected : %@",self.reservationDateSelected);
}

Upvotes: 4

Views: 5774

Answers (2)

Alexey Kozhevnikov
Alexey Kozhevnikov

Reputation: 4259

1) You should use yyyy, not YYYY. YYYY stands for year in "Week of Year" based calendars - it may not always be the same value as calendar year. See the documentation.

2) Format to parse "Monday 02,Dec 2013" should be "EEEE dd,MMM yyyy"

EDIT 3) Forget to say about locale — device with non-english locale will fail to parse "Monday 02,Dec 2013" (because of "Monday" and "Dec") if you will not set the locale for the date formatter explicitly:

NSDateFormatter *dateFormatterForGettingDate = [[NSDateFormatter alloc] init];
dateFormatterForGettingDate.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatterForGettingDate setDateFormat:@"EEEE dd,MMM yyyy"];

I think, this should work:

-(void)dateSelectedInPickerView:(NSString *)dateSelected{

    NSDateFormatter *dateFormatterForGettingDate = [[NSDateFormatter alloc] init];
    dateFormatterForGettingDate.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [dateFormatterForGettingDate setDateFormat:@"EEEE dd,MMM yyyy"];

    // Parse the string representation of the date i.e Monday 2,Dec 2013

    NSDate *dateFromStr = [dateFormatter dateFromString:dateSelected];
    NSLog(@"date selected  : %@",dateFromStr);

    NSDateFormatter *tempFormatter=[[NSDateFormatter alloc]init];
    [tempFormatter setDateFormat:@"yyyy-MM-dd"];

    self.reservationDateSelected=[tempFormatter stringFromDate:dateFromStr];
    NSLog(@"date Selected : %@",self.reservationDateSelected);
}

Upvotes: 5

falsecrypt
falsecrypt

Reputation: 1616

try this, it works for me with Monday 02,Dec 2013:

-(void)dateSelectedInPickerView:(NSString *)dateSelected{

NSDateFormatter *dateFormatterForGettingDate = [[NSDateFormatter alloc] init];
[dateFormatterForGettingDate setDateFormat:@"EEEE dd,MMM yyyy"];
[dateFormatterForGettingDate setLocale:[NSLocale localeWithLocaleIdentifier:@"EN"]];

// Parse the string representation of the date i.e Monday 2,Dec 2013

NSDate *dateFromStr = [dateFormatterForGettingDate dateFromString:dateSelected];
NSLog(@"date selected  : %@", [dateFromStr descriptionWithLocale:[NSLocale currentLocale]]);
NSDateFormatter *tempFormatter=[[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"yyyy-MM-dd"];

self.reservationDateSelected=[tempFormatter stringFromDate:dateFromStr];
NSLog(@"date Selected : %@",self.reservationDateSelected);

}

Read this, if want to know more about Date Format Patterns ;-)

Upvotes: 7

Related Questions