我需要增加15分钟的时间内容,这是我在网络服务中得到的。 代码如下
NSString *dateString =obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:@" %@ +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDate *scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter release];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSString *strTime = [dateFormatter1 stringFromDate:scheduledForYellow];
[dateFormatter1 release];
NSArray *aryy = [strTime componentsSeparatedByString:@" "];
NSLog(@"end time ======>>>%@",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
这里添加15分钟到我的开始时间我已经写了上面的代码,但在结束时间[aryy objectAtIndex:1]没有给我正确的时间。我想做的是增加15分钟的任何约会开始日期。不知道问题是什么。
`
答案 0 :(得分:2)
必须是因为日期格式化程序用来生成字符串的默认时区(本地时区)。我稍微修改了您的代码以重用日期格式化程序并使用dateFromString
修复了泄漏。
NSString *dateString = obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:@" %@ +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate * dateFromString = [dateFormatter dateFromString:dateString];
NSDate * scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString * strTime = [dateFormatter dateFromString:scheduledForYellow];
NSArray * aryy = [strTime componentsSeparatedByString:@" "];
NSLog(@"end time ======>>>%@",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
这应该可以解决问题。如果您遇到任何问题,请告诉我。