2011-07-10 10 views
15

NSDateのhh、mm、ssコンポーネントを別のNSDateのコンポーネントに変更しようとしていますが、何らかの理由で何らかの理由で時刻が真夜中に設定されています。私は理由を理解できません!NSDateComponentsのNSDate

ここにコードがあります。

+ (NSDate *) fixTime:(NSDate*)fromDate toDate:(NSDate *) toDate { 
    NSCalendar *cal = [NSCalendar currentCalendar]; 
    [cal setTimeZone:[NSTimeZone localTimeZone]]; 
    [cal setLocale:[NSLocale currentLocale]]; 
    NSLog(@"fromDate=<%@>", [fromDate descriptionWithLocale:[NSLocale currentLocale]]); 
    NSLog(@"toDate= <%@>", [toDate descriptionWithLocale:[NSLocale currentLocale]]); 

    NSDateComponents *fromTimeComponents = [cal components:(NSHourCalendarUnit | 
                  NSMinuteCalendarUnit | 
                  NSSecondCalendarUnit ) fromDate:fromDate];        
    NSDateComponents *toDateComponents = [cal components:(NSYearCalendarUnit | 
                  NSMonthCalendarUnit | 
                  NSDayCalendarUnit  ) fromDate:toDate]; 
    NSDateComponents *toTimeComponents = [cal components:(NSHourCalendarUnit | 
                  NSMinuteCalendarUnit | 
                  NSSecondCalendarUnit ) fromDate:toDate]; 
    [toTimeComponents setHour:[fromTimeComponents hour]]; 
    [toTimeComponents setMinute:[fromTimeComponents minute]]; 
    [toTimeComponents setSecond:[fromTimeComponents second]]; 
    NSLog(@"toDateComponents year = %d", [toDateComponents year]);              
    NSLog(@"toDateComponents mon = %d", [toDateComponents month]);              
    NSLog(@"toDateComponents day = %d", [toDateComponents day]); 
    NSLog(@"toTimeComponents hour = %d", [toTimeComponents hour]);              
    NSLog(@"toTimeComponents min = %d", [toTimeComponents minute]);              
    NSLog(@"toTimeComponents sec = %d", [toTimeComponents second]); 

    NSDate *newDate = [cal dateFromComponents:toDateComponents]; 
    NSLogDebug(@"newDate=<%@>", [newDate descriptionWithLocale:[NSLocale currentLocale]]); 
    return [[newDate retain] autorelease]; 
} 

ここで、コンソールのデバッグ出力を示します。時間、分、秒の構成要素とNEWDATEの最終値の値を注意:予め

<Util.m:229> fromDate=<Monday, July 11, 2011 10:35:00 PM Pacific Daylight Time> 
    <Util.m:230> toDate= <Saturday, July 23, 2011 9:35:00 PM Pacific Daylight Time> 

    <Util.m:255> toDateComponents year = 2011 
    <Util.m:256> toDateComponents mon = 7 
    <Util.m:257> toDateComponents day = 23 
    <Util.m:258> toTimeComponents hour = 22 
    <Util.m:259> toTimeComponents min = 35 
    <Util.m:260> toTimeComponents sec = 0 
    <Util.m:266> newDate=<Saturday, July 23, 2011 12:00:00 AM Pacific Daylight Time> 

ありがとう。

あなたがここに toDateComponentsオフ日付を生成している

答えて

30

NSDate *newDate = [cal dateFromComponents:toDateComponents]; 

toDateComponents内の情報のみ日付、月、年です。それは時間の詳細がありません。だから12:00:00 AMになる。時間情報はtoTimeComponentsに保存されていますが、使用されていません。これを有効にしたい場合は、toDateComponentsに時間情報を追加し、おそらくtoTimeComponents変数を完全に削除する必要があります。

[toDateComponents setHour:[fromTimeComponents hour]]; 
[toDateComponents setMinute:[fromTimeComponents minute]]; 
[toDateComponents setSecond:[fromTimeComponents second]]; 
+1

ありがとうございました! – Sam