NSDate / NSDateComponent问题

时间:2010-07-15 19:12:36

标签: iphone nsdate nsdatecomponents

假设我们是2010年7月15日,我想获得前一周的第一天,也就是7月5日。我有以下方法,但它不起作用。似乎我的工作日属性有问题......

+ (NSDate *)getFirstDayOfPreviousWeek
{
// Get current date
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];

// Get today date at midnight
NSDateComponents *components = [cal components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit) fromDate:now];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSInteger *dayNbr = [components weekday];
NSLog(@"week day:%@",dayNbr); // CRASH
NSDate *todayMidnight = [cal dateFromComponents:components];

// Get first day of previous week midnight
components = [[[NSDateComponents alloc] init] autorelease];
NSInteger *wd = [dayNbr intValue] * -1 + 1;
[components setWeekDay:wd];
[components setWeek:-1];
NSDate *newDate = [cal dateByAddingComponents:components toDate:todayMidnight options:0];
[cal release];

NSLog(@"new date:%@", newDate);

return newDate;
}

你能帮忙吗?

非常感谢,

吕克

2 个答案:

答案 0 :(得分:8)

我认为它应该更容易

- (NSDate *)getFirstDayOfPreviousWeek
{
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now = [NSDate date];
    NSLog(@"Current date: %@", now);

    // required components for today
    NSDateComponents *components = [cal components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now];

    // adjust them for first day of previous week (Monday)
    [components setWeekday:2];
    [components setWeek:([components week] - 1)];

    // construct new date and return
    NSDate *newDate = [cal dateFromComponents:components];
    NSLog(@"New date: %@", newDate);

    return newDate;
}

请注意,此解决方案假定一周的第一天是星期一。

答案 1 :(得分:0)

以下Swift 3 Playground代码显示如何从任何日期获取上一周的第一天:

import Foundation

let calendar = Calendar(identifier: .gregorian)
let intialDate = Date()

// Prepare the new date components
var newDateComponents = calendar.dateComponents([.year, .month, .weekOfMonth], from: intialDate)
newDateComponents.weekOfMonth = newDateComponents.weekOfMonth! - 1 // get previous week
newDateComponents.weekday = 2 // sunday is 1, monday is 2

// Get the new date
let newDate = calendar.date(from: newDateComponents)
print(newDate ?? "Could not retrieve a date")

如果需要重复此操作,可以将代码重构为函数或方法:

import Foundation

func firstDayOfPreviousWeek(from date: Date) -> Date? {
    let calendar = Calendar(identifier: .gregorian)
    var newDateComponents = calendar.dateComponents([.year, .month, .weekOfMonth], from: date)
    newDateComponents.weekOfMonth = newDateComponents.weekOfMonth! - 1
    newDateComponents.weekday = 2
    return calendar.date(from: newDateComponents)
}

因此,以下游乐场代码显示如何从2010年7月15日开始上一周的第一天:

import Foundation

func firstDayOfPreviousWeek(from date: Date) -> Date? {
    let calendar = Calendar(identifier: .gregorian)
    var newDateComponents = calendar.dateComponents([.year, .month, .weekOfMonth], from: date)
    newDateComponents.weekOfMonth = newDateComponents.weekOfMonth! - 1
    newDateComponents.weekday = 2
    newDateComponents.timeZone = TimeZone(abbreviation: "GMT")
    return calendar.date(from: newDateComponents)
}

let calendar = Calendar(identifier: .gregorian)
var july15Components = DateComponents()
july15Components.day = 15
july15Components.month = 7
july15Components.year = 2010
july15Components.timeZone = TimeZone(abbreviation: "GMT")
let july15 = calendar.date(from: july15Components)!
print(july15) // prints 2010-07-15 00:00:00 +0000

let newDate = firstDayOfPreviousWeek(from: july15)
print(newDate ?? "Could not retrieve a date") // prints 2010-07-05 00:00:00 +0000