2011-08-26 40 views
0

我一直在努力與我的功能返回今天的日期,儘可能接近零秒,分鐘和小時。所以我可以在各種交易中重複使用同一日期。obj-C,爲什麼這給了我昨天的約會?

但是,我現在發現我的函數返回昨天的日期?

+ (NSDate *)makeAbsoluteNSDate:(NSDate*)datSource { 

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: 
      NSGregorianCalendar]; 
    [calendar setTimeZone:[NSTimeZone localTimeZone]]; 

    NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | 
      NSMonthCalendarUnit | NSDayCalendarUnit 
      fromDate:datSource]; 
    [dateComponents setHour:0]; 
    [dateComponents setMinute:0]; 
    [dateComponents setSecond:0]; 

    NSDate *today = [calendar dateFromComponents:dateComponents]; 
    [calendar release]; 

    return today; 
} 
+1

第一,顯而易見的問題會你有沒有檢查你的電腦的時區和日期?請驗證兩者是否相同... – DShah

+0

說明檢查您的設備的日期或系統的日期。 –

+0

我正在使用模擬器和我的Mac日期是正確的。 – Jules

回答

5

如果你想獲得今天或昨天午夜時區,你應該得到NSDate指向足夠的時間GMT。在我的情況下,我的時區爲-2小時,因此我的時區爲+0200,因此2011-08-26的午夜我將獲得2011-08-25 22:00。

您必須確定您正在爲NSCalendar設置正確的時區,即[NSTimeZone systemTimeZone]

我爲獲得午夜在我的時區的常規是:

+ (NSDate *)midnightOfDate:(NSDate *)date { 
    NSCalendar *cal = [NSCalendar currentCalendar]; 
    [cal setTimeZone:[NSTimeZone systemTimeZone]]; 

    NSDateComponents *components = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date]; 
    [components setTimeZone:[cal timeZone]]; 

    [components setHour:0]; 
    [components setMinute:0]; 
    [components setSecond:0]; 

    return [cal dateFromComponents:components]; 
} 

所以,在註釋討論後,這裏的中午功能:

+ (NSDate *)middayOfDate:(NSDate *)date { 
    NSCalendar *cal = [NSCalendar currentCalendar]; 
    [cal setTimeZone:[NSTimeZone systemTimeZone]]; 

    NSDateComponents *components = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date]; 
    [components setTimeZone:[cal timeZone]]; 

    [components setHour:12]; 
    [components setMinute:0]; 
    [components setSecond:0]; 

    return [cal dateFromComponents:components]; 
} 
+0

好吧,我試過你的代碼,並在我的機器上當前時間是'2011-08-26 07:16: 05 +0000',這是datasource值在調試中返回的值。函數返回'2011-08-25 23:00:00 +0000',我認爲這就是你的意思。我真正想從這個功能得到'2011-08-26什麼'?謝謝你的幫助。 – Jules

+2

你從哪裏得到的?如果它來自NSDate,則不完全正確。 __NSDate不包含時區信息___ - 打印其內容始終返回+0000。從我的功能的結果我看到你的時區是+0100所以2011-08-25 23:00是正確的。如果你真的想在格林威治標準時間午夜,我想你需要設置''[NSTimeZone timeZoneForSecondsFromGMT:0]'而不是'[NSTimeZone systemTimeZone]'並且它應該沒問題。 – macbirdie

+0

我傳入'date'NSDate * dateToday = [一般makeAbsoluteNSDate:[NSDate日期]]; – Jules

-3

所有你需要的是[NSDate date]當前日期&時間。下面函數

+0

是的,但我想刪除秒,分鐘和小時。 – Jules

-2

使用獲取日期爲昨天:

- (NSDate *)getDate:(NSDate *)date days:(NSInteger)days hours:(NSInteger)hours mins:(NSInteger)mins seconds:(NSInteger)seconds 
{ 
    NSCalendar *cal = [NSCalendar currentCalendar]; 
    NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[[NSDate alloc] init]]; 

    days = (days*24) + hours; 
    [components setHour:days]; 
    [components setMinute:mins]; 
    [components setSecond:seconds]; 

    NSDate *returnDate = [cal dateByAddingComponents:components toDate:date options:0]; 
    return returnDate; 
} 

爲了得到昨天日期通日期= [NSDate的日期],天= -1,小時= 0和分鐘= 0如方法調用的參數。

+0

我想今天,但我昨天得到 – Jules

+0

@hitesh準備好這個問題 – KingofBliss

+0

OK然後通過天= 0 – Hitesh

相關問題