iPhone - 如何检查日期是否为星期一?
我想知道某个日期是否是星期一。
为此,我按照以下方式进行:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
开始吧,这可以工作并通过整数返回天数:7 = 星期六,1 = 星期日,2 = 星期一...
目标 C
SWIFT 4.2
建立您想要检查的日期是星期几
获取当前日历。
现在使用日历来检查构建日期是否为有效日期
您会注意到“weekdayIndex”是一个整数。如果你想让它成为一个字符串,你可以这样做:
你当然可以使用
let date = Date()
,而不是构建自己的日期而是获取当前星期几Here ya go, this works and returns days via ints: 7 = Sat, 1 = Sun, 2 = Mon...
OBJECTIVE C
SWIFT 4.2
Build up the date you want to check what day of the week it is
get the current calendar.
now use the calendar to check if the build up date is a valid date
You'll notice that the 'weekdayIndex' is an integer. If you want to make it a string, you can do something like this:
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