2011-07-15 32 views
1

嗨,我需要增加15分鐘的時間,我在web服務中獲得的內容。 的代碼如下獲取時間的問題

 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]不給我正確的time.What我想要做的是增加15分鐘,無論我在開始日期的任何日期。不知道問題是什麼。

`

回答

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

這應該解決這個問題。如果您遇到任何問題,請告訴我。

+0

雅是這個問題,並感謝您的幫助... – mrugen