我想用我想要的小时数设置NSDate时间:分钟:秒 目前我正在使用NSDate组件,但它没有给出所需的结果
[comps setHour: -hours];
[comps setMinute:0];
[comps setSecond:0];
NSDate *minDate = [calendar_c dateFromComponents:comps];
答案 0 :(得分: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];
}
答案 1 :(得分: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];
需要注意的一点是,当您判断时间是否准确时,您必须注意时区。例如,在我的应用程序中,当您在断点处停止时,您将看到GMT中的时间(因此它看起来与输入时间不同,这是在我当地时间),但是当时间实际显示在屏幕上时应用程序,它被格式化为在本地时区显示。您可能需要考虑这一点,以确定结果实际是否与您期望的不同。
如果这没有帮助,你能详细说明“没有给出理想的结果”吗?它给出了什么结果,与您的期望相比如何?
答案 2 :(得分:2)
是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)
}
}
答案 3 :(得分:0)
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
comps.hour = 0;
comps.minute = 15;
NSDate *date = [calendar dateFromComponents:comps];
iOS 8
答案 4 :(得分:0)
您可以将0设置为小时,分钟和秒。
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"];
答案 5 :(得分:0)
为那些担心弃用警告的人提供的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)
}