我如何在午夜获得一个NSDate对象?

获取代表当天午夜的NSDate对象的最有效方法是什么?

尝试这个:

NSDate *const date = NSDate.date; NSCalendar *const calendar = NSCalendar.currentCalendar; NSCalendarUnit const preservedComponents = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay); NSDateComponents *const components = [calendar components:preservedComponents fromDate:date]; NSDate *const normalizedDate = [calendar dateFromComponents:components]; 

iOS 8中的新API

iOS 8在NSCalendar上包含一个名为startOfDayForDate的新方法,它非常易于使用:

 let startOfToday = NSCalendar.currentCalendar().startOfDayForDate(NSDate()) 

苹果的描述:

此API返回给定date的第一个时间date。 例如,如果想要“今天”的开始,请传入[NSDatedate]。 如果有两个中点,则返回第一个。 如果没有,则返回确实存在的第一个时刻。

更新,关于时区:

由于startOfDayForDate是NSCalendar上的一个方法,它使用NSCalendar的时区。 所以如果我想看看纽约是什么时候,那么今天在洛杉矶开始的时候,我可以这样做:

 let losAngelesCalendar = NSCalendar.currentCalendar().copy() as! NSCalendar losAngelesCalendar.timeZone = NSTimeZone(name: "America/Los_Angeles")! let dateTodayBeganInLosAngeles = losAngelesCalendar.startOfDayForDate(NSDate()) dateTodayBeganInLosAngeles.timeIntervalSince1970 let dateFormatter = NSDateFormatter() dateFormatter.dateStyle = .MediumStyle dateFormatter.timeStyle = .ShortStyle dateFormatter.timeZone = NSTimeZone(name: "America/New_York")! let timeInNewYorkWhenTodayBeganInLosAngeles = dateFormatter.stringFromDate(dateTodayBeganInLosAngeles) print(timeInNewYorkWhenTodayBeganInLosAngeles) // prints "Jul 29, 2015, 3:00 AM" 
 NSCalendar *cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; [cal setTimeZone:[NSTimeZone systemTimeZone]]; NSDateComponents * comp = [cal components:( NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; [comp setMinute:0]; [comp setHour:0]; NSDate *startOfToday = [cal dateFromComponents:comp]; 

如果你的意思是午夜为23:59,那么设置组件的小时为23分钟为59。

您可以使用以下方法获取NSDate的午夜值。

 - (NSDate *)dateAtBeginningOfDayForDate:(NSDate *)inputDate { // Use the user's current calendar and time zone NSCalendar *calendar = [NSCalendar currentCalendar]; NSTimeZone *timeZone = [NSTimeZone systemTimeZone]; [calendar setTimeZone:timeZone]; // Selectively convert the date components (year, month, day) of the input date NSDateComponents *dateComps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:inputDate]; // Set the time components manually [dateComps setHour:0]; [dateComps setMinute:0]; [dateComps setSecond:0]; // Convert back NSDate *beginningOfDay = [calendar dateFromComponents:dateComps]; return beginningOfDay; } 

Thkis从这里被采取网站 。

迅速:

 let cal = NSCalendar.currentCalendar() //tip:NSCalendarUnit can be omitted, but with the presence of it, you can take advantage of Xcode's auto-completion var comps = cal.components(NSCalendarUnit.YearCalendarUnit | .MonthCalendarUnit | .DayCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit | .SecondCalendarUnit, fromDate: NSDate()) comps.hour = 0 comps.minute = 0 comps.second = 0 let midnightOfToday = cal.dateFromComponents(comps)! 

Swift 2.2:

 let cal = NSCalendar.currentCalendar() let comps = cal.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: NSDate()) comps.hour = 0 comps.minute = 0 comps.second = 0 let midnightOfToday = cal.dateFromComponents(comps)! 

Objective-C的:

 NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *comps = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; [comps setHour:0]; [comps setMinute:0]; [comps setSecond:0]; NSDate *midnightOfToday = [cal dateFromComponents:comps]; 

尝试这个:

 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *now = [NSDate date]; NSDateComponents *components = [gregorian components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now]; NSDateFormatter* df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"MM/dd/yyyy HH:mm:ss.SSS"]; NSLog(@"%@",[df stringFromDate:[gregorian dateFromComponents:components]]); 
 NSDate *now = [NSDate date]; NSDate *beginningOfToday = nil; [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&beginningOfToday interval:NULL forDate:now]; 

你可以使用NSCalendardateBySettingHour:minute:second:ofDate:options:

所以这会像做一样简单:

 NSCalendar *calendar = [NSCalendar currentCalendar]; calendar.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; NSDate *midnight = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:[NSDate date] options:0]; 
  let calendar = NSCalendar.currentCalendar() let unitFlags: NSCalendarUnit = [.Year, .Month, .Day, .Minute, .Second] let components = calendar.components(unitFlags , fromDate: NSDate()) components.hour = 0 components.minute = 0 components.second = 0 //Gives Midnight time of today let midnightOfToday = calendar.dateFromComponents(components)! 

从iOS8开始,您可以使用startDayForDate: https : //developer.apple.com/documentation/foundation/nscalendar/1417161-startofdayfordate

所以,为了得到今天的开始: NSDate * midnight; midnight = [[NSCalendar currentCalendar] startOfDayForDate: [NSDate date]]; NSDate * midnight; midnight = [[NSCalendar currentCalendar] startOfDayForDate: [NSDate date]];