如何设置NSDate的时间?

Mub*_*han 8 objective-c nsdate nsdatecomponent

我想用我想要的小时数设置NSDate时间:分钟:当前我正在使用NSDate组件的秒数但它没有给出所需的结果

[comps setHour: -hours];
[comps setMinute:0];
[comps setSecond:0];
NSDate *minDate = [calendar_c dateFromComponents:comps];
Run Code Online (Sandbox Code Playgroud)

mem*_*ons 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;
}
Run Code Online (Sandbox Code Playgroud)

使用上述类别,如果您想要更改时间的现有日期,则执行以下操作:

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];
}
Run Code Online (Sandbox Code Playgroud)


J.R*_*ong 17

你的方法应该工作正常.我需要一个解决此类型问题的方法(设置各个日期组件),以下代码按预期方式工作.我的情况:我想创建一个使用当前日期的日期对象,但是将时间设置为以字符串形式传递的值.

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];
Run Code Online (Sandbox Code Playgroud)

需要注意的一点是,当您判断时间是否准确时,您必须注意时区.例如,在我的应用程序中,当您在断点处停止时,您将看到GMT中的时间(因此它看起来与输入时间不同,这是在我当地时间),但是当时间实际显示在屏幕上时应用程序,它被格式化为在本地时区显示.您可能需要考虑这一点,以确定结果是否与您期望的结果实际不同.

如果这没有帮助,你能详细说明"没有给出想要的结果"吗?它给出了什么结果,与您的预期相比如何?