在iOS中,Webservice日期格式应更改为所需格式
我正在开发一个iOS应用程序。我收到了以下提到的带有日期字段的服务部门的回复。我想以我所需的格式显示日期,但我无法这样做。我试过很多方法在iOS中,Webservice日期格式应更改为所需格式,ios,nsdateformatter,Ios,Nsdateformatter,我正在开发一个iOS应用程序。我收到了以下提到的带有日期字段的服务部门的回复。我想以我所需的格式显示日期,但我无法这样做。我试过很多方法 <DateModified>2014-07-10T17:18:41.447+00:00</DateModified> // Present <DateModified>2014-07-10 17:18:41</DateModified> // Required 试试这个 NS
<DateModified>2014-07-10T17:18:41.447+00:00</DateModified> // Present
<DateModified>2014-07-10 17:18:41</DateModified> // Required
试试这个
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];
输出是什么?第一种格式(当前日期格式)似乎很好,但第二种格式与您想要的格式(必需的日期格式)不匹配不,不,事实并非如此。我使用上述两种方法转换日期。2014-07-10T17:18:41.447+00:00是字符串对象。我想通过在秒后删除最后的零和点来将其转换为2014-07-10 17:18:41。不,恢复不起作用。我已使用您的代码进行了检查,但日期显示比我们指定的时间多5.30小时输入。从该响应日期转换为所需日期时,其未显示正确的日期和时间。因为存在时区差异,您需要根据您的要求进行设置,因此我在回答中添加了该示例
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];