iPhone - 如何检查日期是否为星期一?

发布于 2024-10-13 19:11:45 字数 1002 浏览 4 评论 0原文

我想知道某个日期是否是星期一。

为此,我按照以下方式进行:

#define kDateAndHourUnitsComponents NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

// for test and debug purpose
NSDateComponents* b = [calendar components:kDateAndHourUnitsComponents fromDate:retDate];
int a=[[calendar components:kDateAndHourUnitsComponents fromDate:theDate] weekday];
// -----------

if ([[calendar components:kDateAndHourUnitsComponents fromDate:theDate] weekday] == EKMonday) DoThis....

但这不起作用... a 和 b 不包含任何有用的内容(a 等于 2147483647),

我还想知道 [calendar Components:NSWeekdayCalendarUnit fromDate: 的用途是什么: retDate] 在这种情况下不再有用......

我还发现这证实了该过程: 如何检查今天是一周中的哪一天(即星期二、星期五?)并比较两个 NSDate?

我错过了什么?

I'm trying to find if a date is Monday.

To do this I proceed this way :

#define kDateAndHourUnitsComponents NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

// for test and debug purpose
NSDateComponents* b = [calendar components:kDateAndHourUnitsComponents fromDate:retDate];
int a=[[calendar components:kDateAndHourUnitsComponents fromDate:theDate] weekday];
// -----------

if ([[calendar components:kDateAndHourUnitsComponents fromDate:theDate] weekday] == EKMonday) DoThis....

But this doesn't work... a and b does not contain anything useful (a equals 2147483647),

I also wonder what can be the use of [calendar components:NSWeekdayCalendarUnit fromDate:retDate] that is not useful anymore in that case...

I also found this that confirms the process :
How to check what day of the week it is (i.e. Tues, Fri?) and compare two NSDates?

What did I miss ?

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

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

发布评论

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

评论(1

安人多梦 2024-10-20 19:11:45

开始吧,这可以工作并通过整数返回天数:7 = 星期六,1 = 星期日,2 = 星期一...

目标 C

NSDate* curDate = [NSDate date];
int dayInt = [[[NSCalendar currentCalendar] components: NSWeekdayCalendarUnit fromDate: curDate] weekday];

SWIFT 4.2

建立您想要检查的日期是星期几

let dateComponents = DateComponents(
    year: 2019,
    month: 2 /*FEB*/,
    day: 23 /*23rd day of that month */
)

获取当前日历。

let cal = Calendar.current

现在使用日历来检查构建日期是否为有效日期

guard let date = cal.date(from: dateComponents ) else {
    // The date you build up wasn't a valid day on the calendar
    return
}

//Now that your date is validated, get the day of the week

let weekdayIndex = cal.component(.weekday, from: date)

您会注意到“weekdayIndex”是一个整数。如果你想让它成为一个字符串,你可以这样做:

if let weekdayName = DateFormatter().weekdaySymbols?[ weekdayIndex - 1] {
    print("The weekday for your date is :: \(weekdayName)")
}

你当然可以使用let date = Date(),而不是构建自己的日期而是获取当前星期几

Here ya go, this works and returns days via ints: 7 = Sat, 1 = Sun, 2 = Mon...

OBJECTIVE C

NSDate* curDate = [NSDate date];
int dayInt = [[[NSCalendar currentCalendar] components: NSWeekdayCalendarUnit fromDate: curDate] weekday];

SWIFT 4.2

Build up the date you want to check what day of the week it is

let dateComponents = DateComponents(
    year: 2019,
    month: 2 /*FEB*/,
    day: 23 /*23rd day of that month */
)

get the current calendar.

let cal = Calendar.current

now use the calendar to check if the build up date is a valid date

guard let date = cal.date(from: dateComponents ) else {
    // The date you build up wasn't a valid day on the calendar
    return
}

//Now that your date is validated, get the day of the week

let weekdayIndex = cal.component(.weekday, from: date)

You'll notice that the 'weekdayIndex' is an integer. If you want to make it a string, you can do something like this:

if let weekdayName = DateFormatter().weekdaySymbols?[ weekdayIndex - 1] {
    print("The weekday for your date is :: \(weekdayName)")
}

Instead of building your own date, you are of course allowed to use let date = Date() instead to get what the current day of the week is

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