我怎样才能在午夜买到今天一个NSDate对象?

问题描述 投票:59回答:10

什么是最有效的方式,以获得代表当天午夜一个NSDate对象?

objective-c cocoa-touch nsdate
10个回答
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];

0
投票
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];

NSDate *midnight = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:[NSDate date] options:0];

97
投票

在iOS的8个新的API

iOS 8的包括上NSCalendar一种新的方法叫做startOfDayForDate,这是很容易使用:

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

苹果的描述:

此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"

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。


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;
}

这是从这里qazxsw POI拍摄。


8
投票

迅速:

website

雨燕2.2:

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)!

Objective-C的:

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)!

3
投票

从iOS8上,你可以使用startDayForDate。

因此,为了得到今天(目标-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];

1
投票

尝试这个:

NSDate * midnight;
 midnight = [[NSCalendar currentCalendar] startOfDayForDate: [NSDate date]];

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]]);

0
投票

你可以使用NSCalendar的NSDate *now = [NSDate date]; NSDate *beginningOfToday = nil; [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&beginningOfToday interval:NULL forDate:now];

因此,这将是因为这样做很容易:

dateBySettingHour:minute:second:ofDate:options:
© www.soinside.com 2019 - 2024. All rights reserved.