Web服务日期格式应更改为iOS中所需的格式

时间:2014-07-10 12:19:04

标签: ios nsdateformatter

我正在开发一款iOS应用。我得到了下面提到的日期字段服务的回复。我想以我要求的格式显示日期,但我无法做到这一点。我尝试了很多方法。

<DateModified>2014-07-10T17:18:41.447+00:00</DateModified>    // Present
<DateModified>2014-07-10 17:18:41</DateModified>            // Required

上面提到的日期应该以所需的格式显示。这是我目前使用的代码。

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'.'SSSZ"];
NSDate *date = [dateFormat dateFromString:myString];

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSString *localDateString = [dateFormatter stringFromDate:date];
NSLog(@"Date=%@", localDateString);

1 个答案:

答案 0 :(得分:0)

试试这个

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    [df setDateFormat: @"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Europe/London"]; //set here the timezone you want
    [df setTimeZone:timeZone];

    NSDate *inputDate = [df dateFromString: dateString];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *inputValue = [df stringFromDate:inputDate];