简体   繁体   English

更改日期格式中的问题

[英]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 目前的甲酸盐是MM / dd / yyyy

i want to change it to dd/MM/yyyy 我想把它改成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 我的问题是为什么旧的打印2013-03-04而不是05/03/2013在第一次循环结束它被打印03/05/2013而不是05/03/2013当循环运行第二次它得到new中的null值,这就是为什么在数组程序中添加null值会崩溃的原因

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: 我不是Objective-C开发人员,但我怀疑你只是想:

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.) (您可能需要检查是否date为null,内环路,处理不好的数据,当然。)

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". 首先,您应该将字符串“MM / dd / yyyy”转换为“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];

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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