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 ?
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