2013-04-02 46 views
0

其實在我simulater時間顯示近5個小時30分鐘問題顯示5小時30分鐘過去時

我目前在印度的時間是2013年4月2日09:10 :__ AM但產量是2013年4月2日3點54分51秒 +0000

2013-四月 - 2日:上午9:10

我需要得到兩個日期之間1.首先一些成果當前日期至2.當前日期

即從4月1日至4月2日上午9.10 T0當前時間2013年4月2日09:10 :__ AM

但dispalys

日期一個月的第一天爲2013- 03- 31 18:30:00 +0000

當前時間爲2013- 04-02 03:54:51 +0000 ,但實際時間是09:10:上午__

我COM普特月份的第一次約會如下

// TO IMPLEMENT CODE for FIRST DAY OF the Month 
     NSCalendar* calendar = [NSCalendar currentCalendar]; 
     NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]]; 

calendar.timeZone = [NSTimeZone systemTimeZone]; 

     [comps setWeekday:1]; // 1: sunday 
     monthComponent = comps.month; 
     yearComponent = comps.year; 
     dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"yyyy MM dd"]; 

dateFormatter.timeZone = [NSTimeZone systemTimeZone]; 

     // To get the firstDateOfMonth for This MONTH. 
     NSDate *firstDateOfMonth = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d %d 01",yearComponent,monthComponent]]; 

     NSLog(@"firstDateOfMonth-- %@ \r\n",firstDateOfMonth); 
     NSLog(@"[NSDate date]-- %@",[NSDate date]); 

我設置時區SystemTimeZone甚至認爲i got wrong results for Current date and firstDateOfMonth

在此先感謝

回答

0

問題時區。 默認情況下,NSDateFormatter設置爲您的本地時區。這意味着,如果您的時區爲+5:30,則在1970-03-18 00:00:00 +0530給出日期「1970/18/3」。但是,NSLog始終在GMT(零)時區打印日期,添加/減去時區差異(5小時30分鐘)。

NSCalendar* calendar = [NSCalendar currentCalendar]; 
    NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]]; 


    [comps setWeekday:1]; 
    int monthComponent = comps.month; 
    int yearComponent = comps.year; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy MM dd"]; 

    NSDate *firstDateOfMonth = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d %d 01",yearComponent,monthComponent]]; 
    firstDateOfMonth = [firstDateOfMonth dateByAddingTimeInterval:19800]; 
    NSLog(@"firstDateOfMonth-- %@ \r\n",firstDateOfMonth); 
    NSLog(@"[NSDate date]-- %@",[[NSDate date] dateByAddingTimeInterval:19800]); 
0

試試吧....

NSDate* date = [NSDate date]; 
NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; 
NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone]; 
formatter.timeZone = destinationTimeZone; 
[formatter setDateStyle:NSDateFormatterLongStyle]; 
[formatter setDateFormat:@"MM/dd/yyyy hh:mma"]; 
NSString* dateString = [formatter stringFromDate:date]; 
0

的問題是時區,晴天說,加什麼的NSDate,其實就是一種誤解。

NSDate代表世界各地都一樣的時間點。如果我們同時調用[NSDate date],我們會得到相同的結果,並且NSLog以相同的方式打印它。但是,如果我們都看着手錶,我們會看到不同的時間顯示。

你在印度。如果您在午夜時分觀看手錶,則會顯示12點。如果倫敦的一個人在同一時間看他們的手錶,那個人將在下午6:30看到他們的手錶。他們的手錶總是在你身後5個半小時。這就是NSLog顯示的內容。

例如,您正確計算了印度月初的NSDate。本月初是4月1日,即在印度時間的午夜。但在那個時候,它仍然是三月三十一日在倫敦,在18:30。這就是NSDate顯示的內容。你總是得到正確的結果。