Objective c 如何设置NSDate的时间?
我想用所需的小时:分钟:秒设置NSDate时间 目前,我正在使用NSDate组件,但它没有提供所需的结果Objective c 如何设置NSDate的时间?,objective-c,nsdate,nsdatecomponent,Objective C,Nsdate,Nsdatecomponent,我想用所需的小时:分钟:秒设置NSDate时间 目前,我正在使用NSDate组件,但它没有提供所需的结果 [comps setHour: -hours]; [comps setMinute:0]; [comps setSecond:0]; NSDate *minDate = [calendar_c dateFromComponents:comps]; 你的方法应该行得通。我需要解决这个类型的问题(设置单独的日期组件),下面的代码对我来说工作正常。我的情况:我想创建一个日期对象,该对象使用当前日
[comps setHour: -hours];
[comps setMinute:0];
[comps setSecond:0];
NSDate *minDate = [calendar_c dateFromComponents:comps];
你的方法应该行得通。我需要解决这个类型的问题(设置单独的日期组件),下面的代码对我来说工作正常。我的情况:我想创建一个日期对象,该对象使用当前日期,但将时间设置为作为字符串传入的值
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];
需要注意的一点是,当你判断一个时间是否准确时,你真的必须注意时区。例如,在我的应用程序中,当您在断点处停止时,您将看到以GMT表示的时间(因此它看起来与输入时间不同,输入时间是在我的本地时间),但当时间实际显示在应用程序的屏幕上时,它将被格式化为显示在本地时区中。您可能需要考虑这一点,以确定结果是否与您预期的结果不同
如果这样做无济于事,你能详细说明一下“没有达到预期的结果”吗?它产生了什么样的结果?与您预期的结果相比如何?这是一个非常好的
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分钟:30秒: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];
}
iOS 8
这是Swift2
extension NSDate {
func dateWithHour (hour: Int, minute:Int, second:Int) ->NSDate?{
let calendar = NSCalendar.currentCalendar(),
components = calendar.components([.Day,.Month,.Year], fromDate: self)
components.hour = hour;
components.minute = minute;
components.second = second;
return calendar.dateFromComponents(components)
}
}
您可以将0设置为小时、分钟和秒
Swift 5解决方案(基于@dattk-answer)适用于那些害怕遭到弃用警告的人:)
NSDate设计用于表示实时的日期。您正在尝试为实际日期设置时间组件吗?在您的类别中,dateWithHour:minute:second
日历的来源是什么?@Lukasz'Severiaan'Grela代码中遗漏了一个[NSCalendar currentCalendar]
变量。添加。24小时格式和AM/PM格式都正确吗?例如,如果我这样调用函数:NSDate*newDate=[somedatedatewithhour:6分钟:30秒:00];“6”是早上6点还是下午6点?很明显,如果我使用的是24小时格式,即6点是早上6点。我只是得到了这个错误:2015-01-16 12:14:05.864 teInsta[26792:4444831]***-[\uu NSCFCalendar components:fromDate:]:date不能为零未来异常。这些错误中的一些将在本投诉中报告,然后进一步的违规行为将被忽略。下面是这次出现这种情况的回溯(由于编译器优化,某些帧可能会丢失):(鉴于此答案在三年半前发布时效果很好,向下投票似乎有点苛刻。您是从上面的答案还是从代码中得到此异常?根据您报告的内容,请看这里:
extension NSDate {
func dateWithHour (hour: Int, minute:Int, second:Int) ->NSDate?{
let calendar = NSCalendar.currentCalendar(),
components = calendar.components([.Day,.Month,.Year], fromDate: self)
components.hour = hour;
components.minute = minute;
components.second = second;
return calendar.dateFromComponents(components)
}
}
NSDateFormatter *tFmt = [[NSDateFormatter alloc] init];
tFmt.dateFormat = @"yyyy-MM-dd";
NSString *strNowDate = [NSString stringWithFormat:@"%@ 00:00:00",[tFmt stringFromDate:[NSDate date]]];
NSDate *nowDate = [NSDate dateWithString:strNowDate formatString:@"yyyy-MM-dd HH:mm:ss"];
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)
}