8

NSDateコンポーネントを現在使用している希望の時間:分:秒でNSDate時刻を設定したいのですが、希望する結果が得られません

[comps setHour: -hours];
[comps setMinute:0];
[comps setSecond:0];
NSDate *minDate = [calendar_c dateFromComponents:comps];
4

6 に答える 6

22

これはNSDateカテゴリーとしてうまく機能します。

/** Returns a new NSDate object with the time set to the indicated hour, 
  * minute, and second.
  * @param hour The hour to use in the new date.
  * @param minute The number of minutes to use in the new date.
  * @param second The number of seconds to use in the new date.
  */
-(NSDate *) dateWithHour:(NSInteger)hour 
                  minute:(NSInteger)minute 
                  second:(NSInteger)second 
{
   NSCalendar *calendar = [NSCalendar currentCalendar];
   NSDateComponents *components = [calendar components: NSYearCalendarUnit|
                                                         NSMonthCalendarUnit|
                                                         NSDayCalendarUnit
                                               fromDate:self];
    [components setHour:hour];
    [components setMinute:minute];
    [components setSecond:second];
    NSDate *newDate = [calendar dateFromComponents:components];
    return newDate;
}

上記のカテゴリで、時間を変更したい既存の日付がある場合は、次のようにします。

NSDate *newDate = [someDate dateWithHour:10 minute:30 second:00];

ただし、既存の日付から時間を加算または減算しようとしている場合は、それを行うためのカテゴリ メソッドも簡単です。

/** Returns a new date with the given number of hours added or subtracted.
  * @param hours The number of hours to add or subtract from the date.
  */
-(NSDate*)dateByAddingHours:(NSInteger)hours
{
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setHour:hours];

    return [[NSCalendar currentCalendar] 
               dateByAddingComponents:components toDate:self options:0];
}
于 2013-08-14T19:00:56.253 に答える
18

あなたのアプローチはうまくいくはずです。このタイプの問題 (個々の日付コンポーネントの設定) の解決策が必要でした。次のコードは期待どおりに機能します。私の状況: 現在の日付を使用し、文字列として渡された値に設定された時刻を持つ日付オブジェクトを作成したかったのです。

NSString *string = @"7:00";
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *timeOnlyFormatter = [[NSDateFormatter alloc] init];
[timeOnlyFormatter setLocale:locale];
[timeOnlyFormatter setDateFormat:@"h:mm"];

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *today = [NSDate date];
NSDateComponents *todayComps = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:today];

NSDateComponents *comps = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[timeOnlyFormatter dateFromString:string]];
comps.day = todayComps.day;
comps.month = todayComps.month;
comps.year = todayComps.year;
NSDate *date = [calendar dateFromComponents:comps];
[calendar release];
[timeOnlyFormatter release];
[locale release];

注意すべきことの 1 つは、時間が正確に見えるかどうかを判断するときは、タイム ゾーンに注意を払う必要があるということです。たとえば、私のアプリでは、ブレークポイントで停止すると、GMT で時間が表示されます (そのため、ローカル時間である入力時間とは異なって見えます)。アプリで、ローカル タイムゾーンで表示するようにフォーマットされています。結果が実際に期待したものと異なるかどうかを判断するには、これを考慮する必要がある場合があります。

これで解決しない場合は、「望ましい結果が得られない」ことについて詳しく説明していただけますか? それはどのような結果をもたらし、それはあなたが期待したものとどのように比較されますか?

于 2011-09-25T16:24:26.287 に答える
1

非推奨の警告を恐れる人のためのSwift 5ソリューション(@dattkの回答に基づく):)

func date(withHour hour: Int, withMinute minute: Int, withSeconds second: Int) -> Date? { 
   let now = Date() 
   let calendar = NSCalendar.current 
   var components = calendar.dateComponents([.day,.month,.year], from: now)   
   components.hour = hour 
   components.minute = minute 
   components.second = second 
   return calendar.date(from: components) 
}
于 2019-06-28T19:02:46.733 に答える