Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue in Changing date formate

Tags:

ios

iphone

i have one array of string something like below

newlymadeArray(
"03/05/2013",
"09/22/2013",
"03/02/2013",
"03/02/2013",
"08/22/2013",
"11/02/2013",
"07/08/2013",
"08/31/2013",
"05/13/2013",
"07/11/2013",
"10/07/2013",
"02/20/2013"

)

current formate is MM/dd/yyyy

i want to change it to dd/MM/yyyy

i am using following code for this

  NSLog(@"_newlymadeArray%@",_newlymadeArray);
//logic for changing date formate
for( int i=0; i<_newlymadeArray.count; i++){
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"MM/dd/yyyy"];
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
    [dateFormatter2 setDateFormat:@"dd/MM/yyyy"];

    NSDate *old = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
    NSLog(@"old %@",old);

    NSString *old1 =[dateFormatter1 stringFromDate:old];

    NSDate *new = [dateFormatter2 dateFromString:old1];
    NSLog(@"new %@",new);

    NSString *string = [dateFormatter2 stringFromDate:new];
    NSLog(@"string %@",string);

    [_convertedBdates addObject:string];

        }

      NSLog(@"_convertedBdates%@",_convertedBdates);

this is what im getting as output

  old 2013-03-04 18:30:00 +0000
  new 2013-05-02 18:30:00 +0000
  string 03/05/2013
  old 2013-09-21 18:30:00 +0000
  new (null)
 string (null)
 ] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:     '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
 *** First throw call stack:

my questions are why old get printed 2013-03-04 instead of 05/03/2013 at the end of first time loop it gets printed 03/05/2013 instead of 05/03/2013 and when loop is running second time it gets null value in new so thats why adding null value in array program get crashed

plz tell me what im doing wrong ?

like image 427
user1660882 Avatar asked Jul 04 '26 08:07

user1660882


2 Answers

This is never going to be appropriate:

NSString *old1 =[dateFormatter1 stringFromDate:old];
NSDate *new = [dateFormatter2 dateFromString:old1];

You're saying, "Format a date using one formatter... and then parse it using a different one."

You've already parsed the value which was in the old format. You now need to format it in the new formatter.

I'm not an Objective-C developer, but I suspect you just want:

NSDate *date = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
NSString *string = [dateFormatter2 stringFromDate:date];
[_convertedBdates addObject:string];

Note how there's only one date involved, but two strings (the original format and the new format).

Also note that there's no need to create a new pair of formatters on each iteration of the loop. Create them before the loop and reuse them:

NSDateFormatter *oldFormatter = [[NSDateFormatter alloc] init];
[oldFormatter setDateFormat:@"MM/dd/yyyy"];
NSDateFormatter *newFormatter = [[NSDateFormatter alloc] init];
[newFormatter setDateFormat:@"dd/MM/yyyy"];

for (int i = 0; i < _newlymadeArray.count; i++) {
    NSDate *date = [oldFormatter dateFromString:[_newlymadeArray objectAtIndex:i]];
    NSString *string = [newFormatter stringFromDate:date];
    [_convertedBdates addObject:string];
}

(You may also want to check whether date is null within the loop, to handle bad data, of course.)

like image 145
Jon Skeet Avatar answered Jul 06 '26 23:07

Jon Skeet


Try this one:

NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy"];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"dd/MM/yyyy"];
for( int i=0; i<_newlymadeArray.count; i++){

    NSDate *old = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
    NSLog(@"old %@",old);

    if (old) {
        NSString *newDateString = [dateFormatter2 stringFromDate:old];
        NSLog(@"new %@",newDateString);
        [_convertedBdates addObject:newDateString];
    }
}
[dateFormatter1 release];
[dateFormatter2 release];
like image 38
Evgeniy Gushchin Avatar answered Jul 06 '26 22:07

Evgeniy Gushchin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!