2

假设我们是 15/07/2010,我想获得前一周的第一天,即 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;
}

能否请你帮忙 ?

非常感谢,

卢克

4

2 回答 2

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

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

于 2010-07-15T20:52:09.230 回答
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)
}

因此,下面的 Playground 代码展示了如何从 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
于 2016-12-17T16:27:55.277 回答