NSDate / NSDateComponent 问题

发布于 2024-09-09 17:42:24 字数 1141 浏览 1 评论 0原文

假设我们是 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;
}

您能帮忙吗?

非常感谢,

吕克

Let's say we are the 15/07/2010, I would like to get the first day of the previous week, which is the 5 of July. I have the following method but it does not work. Seems that I have a problem with the weekday attribute...

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

Could you please help ?

Thanks a lot,

Luc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同展鸳鸯锦 2024-09-16 17:42:24

我认为这应该容易得多,

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

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

I think it should be a lot easier

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

Note this solution assumes the first day of the week is Monday.

£烟消云散 2024-09-16 17:42:24

以下 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

The following Swift 3 Playground code shows how to get the first day of the previous week from any date:

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

If you need to repeat this operation, you may refactor your code into a function or a method:

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

Therefore, the following Playground code shows how to get the first day of the previous week from July 15, 2010:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文