获取表示当天午夜的NSDate对象的最有效方法是什么?
答案 0 :(得分:96)
iOS 8中的新API
iOS 8在NSCalendar上包含一个名为 startOfDayForDate 的新方法,它非常易于使用:
let startOfToday = NSCalendar.currentCalendar().startOfDayForDate(NSDate())
Apple的描述:
此API返回给定日期的第一个时刻日期。 例如,如果您想要“今天”的开头,请传入[NSDate日期]。 如果有两个中午,则返回第一个。如果没有,则返回确实存在的第一个时刻。
更新,关于时区:
由于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"
答案 1 :(得分:65)
试试这个:
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];
答案 2 :(得分:12)
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];
[comp setSecond:0];
NSDate *startOfToday = [cal dateFromComponents:comp];
如果您指的是午夜时间为23:59,则将组件的小时设置为23,将分钟设置为59。
答案 3 :(得分:8)
您可以使用以下方法获取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取自website。
答案 4 :(得分:8)
夫特:
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)!
目标-C:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSDate *midnightOfToday = [cal dateFromComponents:comps];
答案 5 :(得分:3)
从iOS8开始,您可以使用startDayForDate。
所以,为了获得今天的开始(目标-C):
NSDate * midnight;
midnight = [[NSCalendar currentCalendar] startOfDayForDate: [NSDate date]];
答案 6 :(得分:1)
试试这个:
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]]);
答案 7 :(得分:1)
NSDate *now = [NSDate date];
NSDate *beginningOfToday = nil;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&beginningOfToday interval:NULL forDate:now];
答案 8 :(得分: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)!
答案 9 :(得分:0)
您可以使用 NSCalendar ' dateBySettingHour: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];