2013-10-21 163 views
0

相同數量我沒有建立一個自定義日曆,當我經過一週改變了一天的日數,我看到了奇怪的情況時的日期是26日,但NSDateFormatter和NSDateComponents返回27,同樣是27 = 27,你可以看到一個日誌。在另一種情況下,31是30,而1是31NSDateFormatter或NSDateComponents返回不同的日期

有誰有這樣的事情嗎?

for (NSDictionary *dict in weekViews) { 
    UILabel *dayName = dict[@"dayName"]; 
    UILabel *dayNumber = dict[@"dayNumber"]; 

    components = [calendar components:NSCalendarUnitMonth | NSCalendarUnitDay fromDate:tempDate]; 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 

    formatter.dateFormat = @"dd"; 

    //TODO:Remove NSLog 
    NSLog(@"%@ = %@", [formatter stringFromDate:tempDate], tempDate); 

    dayName.text = [NSString stringWithFormat:@"%@", weekDaySymbols[index++]]; 
    dayNumber.text = [NSString stringWithFormat:@"%d", components.day]; 

    tempDate = [tempDate dateByAddingTimeInterval:86400]; 
} 

2013年10月22日01:06:25.812護理[539:70B] 27 = 2013年10月26日21點00分零零秒0000

2013年10月22日01:06: 25.813護理[539:70b] 27 = 2013-10-27 21:00:00 +0000

2013-10-22 01:06:25.813護理[539:70b] 28 = 2013-10-28 21: 00:00 0000

2013年10月22日01:06:25.814護理[539:70B] 29 = 2013年10月29日21點00分00秒0000

2013年10月22日01: 06:25.814關心[539:70B] 30 =二○一三年十月三十○日二十一點○○分00秒0000

2013年10月22日01:06:25.815護理[539:70B] 31 = 2013年10月31日21時:00 0000

2013年10月22日01:06:25.816護理[539:70B] 01 = 2013年11月1日21點00分00秒0000

該代碼的另一個例子:

2013-10-22 01:14:12.456護理[539:70b] 22 = 2014-03-22 21:00:00 +0000

2013-10-22 01:14:12.456護理[539: 70b] 23 = 2014-03-23 21:00:00 +0000

2013年10月22日01:14:12.457護理[539:70B] 24 = 2014年3月24日21時00分00秒0000

2013年10月22日01:14:12.457護理[539 :70B] 25 = 2014年3月25日21點00分○○秒0000

2013年10月22日01:14:12.458護理[539:70B] 26 = 2014年3月26日21點00分○○秒+ 0000

2013年10月22日01:14:12.458護理[539:70B] 27 = 2014年3月27日21時00分○○秒0000

2013年10月22日01:14:12.459護理[539:70b] 28 = 2014-03-28 21:00:00 +0000

2013-10-22 01:12:34.942護理[539:70b] 29 = 2014-03-29 21:00:00 +0000

2013-10-22 01:12:34.943護理[539: 70B] 31 = 2014年3月30日21時00分○○秒0000

2013年10月22日01:12:34.943護理[539:70B] 01 = 2014-03-31二十一點00分00秒0000

2013年10月22日01:12:34.944護理[539:70B] 02 = 2014年4月1日二十一時00分00秒0000

2013年10月22日01:12:34.944護理[ 539:70b] 03 = 2014-04-02 21:00:00 +0000

2013 -10-22 01:12:34.945護理[539:70B] 04 = 2014年4月3日21點00分00秒0000

2013年10月22日1點12分三十四秒。945護理[539:70B] 05 = 2014年4月4日21:00:00 +0000

我沒有打破我的大腦:)

+0

時區!!!!!! –

+0

(夏令時何時結束/從哪裏開始?) –

+0

(如果您不知道,烏克蘭的「夏令時」在10月的最後一個星期日結束,並在3月的最後一個星期日再次開始。) –

回答

-4

我想你應該爲您的NSDateFormatter對象日曆,試試這個:

[formatter setCalendar:calendar]; 

您應該在呼叫stringFromDate:方法之前設置日曆。

+0

我確實嘗試了一切:locale,日曆,時區...我嘗試使用NSDateFormatter和NSDataComponents,結果相同。 – invoodoo

+0

@AntonAnisimov,你是如何創建日曆的?你能發佈完整的代碼嗎? – JeroValli

+0

我剛剛發現,10月27日這是一個DST,如果我有00:00的時間,由於DST,它會變成23:00。日曆本身無關緊要。如果我使用白天例如13:00就沒有問題,但是我發現NSDateComponents更適合於意識到它很好。謝謝你的幫助! – invoodoo

1

是的,它與DST(夏令時)有關。我剛剛使用NSDateFormat替換了NSDateComponent的一天增量。

NSDate *tempDate = currentDate; 

for (NSDictionary *dict in weekViews) { 
    UILabel *dayName = dict[@"dayName"]; 
    UILabel *dayNumber = dict[@"dayNumber"]; 

    components = [calendar components:NSCalendarUnitMonth | NSCalendarUnitDay fromDate:tempDate]; 

    dayName.text = [NSString stringWithFormat:@"%@", weekDaySymbols[index++]]; 
    dayNumber.text = [NSString stringWithFormat:@"%d", components.day]; 

    components.day++; 

    tempDate = [calendar dateFromComponents:components]; 
} 

DST很近,我發現這個bug在今天提交應用程序之前。

感謝大家。我想這將在未來幫助某人。

相關問題