简体   繁体   中英

issue in Changing date formate

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 ?

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.)

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];
NSDateFormatter *fromDateFormatter = [[NSDateFormatter alloc] init];
[fromDateFormatter setDateFormat:@"MM/dd/yyyy"];

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

for (NSString *dateString in newlyMadeArray)
{
    NSDate *date = [fromDateFormatter dateFromString:dateString];
    NSString *newDateString = [toDateFormatter stringFromDate:date];
    [_convertedBdates addObject:newDateString];
}

First you should convert the string "MM/dd/yyyy" to "dd/MM/yyyy".

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);



NSArray *arr = [[_newlymadeArray objectAtIndex:i] componentsSeparatedByString:@"/"];
    NSString *old1 = [NSString stringWithFormat:@"%@/%@/%@",[arr objectAtIndex:1],[arr objectAtIndex:0],[arr objectAtIndex:2]];
    NSDate *new = [dateFormatter2 dateFromString:old1];
    NSLog(@"new %@",new);

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

    [_convertedBdates addObject:string];

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM