如何设置现有的NSDate
时间?
那就是我有一个约会(比如说当前日期/时间),但是想把它设置到特定时间(例如11.23am)说。目标C中最快的方法是什么?
我正在查看NSCalendar
并查看dateByAddingComponents:toDate:options:
和dateFromComponents:
等方法,但这些方法似乎并不符合我的需求。例如:
dateFromComponents:
- 我必须弄清楚所有这些组件才能起作用,这似乎有点过分dateByAddingComponents:toDate:options:
- 我不想在我的情况下添加,而是“设定”时间。答案 0 :(得分:70)
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: 7];
[components setMinute: 59];
[components setSecond: 17];
NSDate *newDate = [gregorian dateFromComponents: components];
我使用NSUIntegerMax
而不是特定的OR组合位掩码,因为它更容易,但如果您想指定要接收哪些数据组件,请成为我的访客。
答案 1 :(得分:11)
iOS 7注意:我将这个问题放在其中一些问题中,因为使用NSUIntegerMax似乎不再适用于iOS7,使用它会让我追尾约3小时。我刚刚发现,当我使用NSUIntegerMax
时,NSDateComponents
会忽略设定年份。例如,这不会改变年份:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:NSUIntegerMax fromDate:date];
[comps setYear:year];
return [gregorian dateFromComponents:comps];
虽然这样做:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger timeComps = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit);
NSDateComponents *comps = [gregorian components:timeComps fromDate:date];
[comps setYear:year];
return [gregorian dateFromComponents:comps];
这可能是一个iOS7错误,或者使用NSUIntegerMax工作可能是一个不再有效的侥幸。我将提交错误报告并获得明确的答案。
无论如何,如果你想避免意想不到的后果,请指明你可能正在追逐尾巴的所有其他组件!
答案 2 :(得分:8)
Swift 2.0版本如何将给定日期设置为早上8点
extension NSDate {
func setTo8AM() -> NSDate {
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let components = calendar.components(([.Day, .Month, .Year]), fromDate: self)
components.hour = 8
return calendar.dateFromComponents(components)!
}
}
答案 3 :(得分:3)
应用此选项将时间从另一个日期设置为现有日期
NSDate *today = [NSDate date]; // from which i need only "Year" "Month" "Date"
NSCalendar *calendar = [NSCalendar currentCalendar]
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:today];
NSDate *timeDate = datePicker.date; // from which i need only "Hours" "Minutes" and "Seconds"
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:timeDate];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
[dateComponents setSecond:[timeComponents second]];
NSDate *newDate = [calendar dateFromComponents:dateComponents]; // New Date with My "Year" "Month" "Date" "Hours" "Minutes" and "Seconds"
NSLog(@"New Date: %@",newDate);
答案 4 :(得分:3)
这是一个更通用的Swift(v2.1.1)扩展,它以Andrew Schreiber的有用答案为基础。
int[][] my2DArray = new int[17][4];
答案 5 :(得分:1)
一个迅速的例子。为了我的目的,我想明天中午12点左右。
var tomorrow:NSDate = NSDate().dateByAddingTimeInterval(60*60*24); //60seconds*60minutes*12hours
var gregorian:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!;
var unit : NSCalendarUnit = (NSCalendarUnit.YearCalendarUnit|NSCalendarUnit.MonthCalendarUnit|NSCalendarUnit.DayCalendarUnit|NSCalendarUnit.HourCalendarUnit|NSCalendarUnit.MinuteCalendarUnit);
var comps:NSDateComponents = gregorian.components(unit, fromDate: tomorrow);
comps.setValue(12, forComponent: NSCalendarUnit.HourCalendarUnit);
comps.setValue(0, forComponent: NSCalendarUnit.MinuteCalendarUnit);
var noon_tomorrow : NSDate = gregorian.dateFromComponents(comps)!;