3

我正在努力弄清楚如何在上午 12:00 为上一个星期日动态创建日期对象

我在想我可以得到今天的日期,然后减去一周中的当前日期 + 1,此时我可以减去一天中的时间,直到凌晨 12 点。

到目前为止,我有一周中的当前日期:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [comps weekday];

在这一点上,我可以得到今天的日期并减去weekday一天中 * 秒的差异。但是,我怎样才能以秒为单位获得今天的时间?

4

4 回答 4

7

无需手动计算秒数(由于夏令时等原因,这无论如何都是危险的)。以下应该完全符合您的要求:

NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]]; // force US locale, because other countries (e.g. the rest of the world) might use different weekday numbering

NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];

[nowComponents setWeekday:1]; //Sunday
[nowComponents setHour:0]; // 12:00 AM = midnight (12:00 PM would be 12)
[nowComponents setMinute:0];
[nowComponents setSecond:0];

NSDate *previousSunday = [calendar dateFromComponents:nowComponents];
于 2012-07-19T16:34:37.340 回答
2

我将担心夏令时之间的转换留给您:

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *date = [NSDate date];
NSLog(@"date %@", date);

NSDateComponents *componentsToday = [gregorian components:NSWeekdayCalendarUnit fromDate:date]; // NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | 
NSInteger days = componentsToday.weekday - 1;
NSLog(@"Days=%d", days);

NSDate *lastSunday = [date dateByAddingTimeInterval:-days*60*60*24];
NSLog(@"lastSunday %@", lastSunday);

NSDateComponents *componentsSunday = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit  fromDate:lastSunday];
[componentsSunday setHour:0];
[componentsSunday setMinute:0];
[componentsSunday setSecond:0];
NSDate *targetDate = [gregorian dateFromComponents:componentsSunday];
NSLog(@"TargetDate %@", targetDate);
于 2012-07-19T17:02:19.203 回答
2

Swift 2.0 中的@AndreasLey 解决方案:

  func getLastSunday() -> NSDate?
{
    let today = NSDate();
    let calendar : NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
    calendar.locale = NSLocale(localeIdentifier: "en-US") // force US locale, because other countries (e.g. the rest of the world) might use different weekday numbering

    let flags : NSCalendarUnit = [.Year , .Month, .Weekday, .WeekOfMonth, .Minute , .Second]
    let nowComponents = calendar.components(flags, fromDate: today)

    nowComponents.weekday = 1 //Sunday
    nowComponents.hour = 0  // 12:00 AM = midnight (12:00 PM would be 12)
    nowComponents.minute = 0
    nowComponents.second = 0;


    let  previousSunday = calendar.dateFromComponents(nowComponents);
    return previousSunday
}
于 2016-02-24T10:07:01.103 回答
0

此代码在您从 UIDatePicker 选择的日期设置为上午 12:00。

 NSDate *oldDateSelected = datePicker.date;
 unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:unitFlags fromDate:oldDateSelected];
    comps.hour   = 00;
    comps.minute = 00;
    comps.second = 44;
    NSDate *newDate = [calendar dateFromComponents:comps];
    return newDate;
于 2014-09-26T06:14:55.257 回答