How to get all days in a month that are a specific weekday?

SPUDERMAN

How do I get the actual date of the month based on a given a day ? For example, I would like to retrieve all the dates in June 2017 which are Saturday. How can I achieve that ? Sample code will be very much appreciated as I have struggled for days on this.

rob mayoff

A DateComponents has a weekday property, representing the day of the week. The weekdays are (in Foundation's Gregorian calendar) numbered 1 for Sunday, 2 for Monday, …, and 7 for Saturday.

A DateComponents also has a weekdayOrdinal property, representing “the position of the weekday within the next larger calendar unit, such as the month. For example, 2 is the weekday ordinal unit for the second Friday of the month.

So let's initialize a DateComponents for some Saturday in June 2017. It's generally a good idea to specify a time of noon if you don't care about the time, because midnight (the default time of day) can cause problems in some time zones on some days.

var components = DateComponents(era: 1, year: 2017, month: 06, hour: 12, weekday: 7)

And let's make a calendar.

var calendar = Calendar.autoupdatingCurrent

Now we can loop over all the possible weekday ordinals. For each, we'll ask the calendar to generate a date. Then we ask the calendar to convert the date back to year, month, and day components.

In the Gregorian calendar, some months have 5 Saturdays, but most have 4. So when we ask for the 5th Saturday, we'll probably get a date in the following month. When that happens, we want to suppress that date.

for i in 1 ... 5 {
    components.weekdayOrdinal = i
    let date = calendar.date(from: components)!
    let ymd = calendar.dateComponents([.year, .month, .day], from: date)
    guard ymd.month == components.month else { break }
    print("\(ymd.year!)-\(ymd.month!)-\(ymd.day!)")
}

Output:

2017-6-3
2017-6-10
2017-6-17
2017-6-24

Objective-C version:

NSDateComponents *components = [NSDateComponents new];
components.era = 1;
components.year = 2017;
components.month = 6;
components.hour = 12;
components.weekday = 7;
NSCalendar *calendar = NSCalendar.autoupdatingCurrentCalendar;

for (NSInteger i = 1; i <= 5; ++i) {
    components.weekdayOrdinal = i;
    NSDate *date = [calendar dateFromComponents:components];
    NSDateComponents *ymd = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
    if (ymd.month != components.month) { break; }
    NSLog(@"%ld-%ld-%ld", (long)ymd.year, (long)ymd.month, (long)ymd.day);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get all days in current month?

From Dev

How to get the number of days in a specific month using Java Calendar?

From Dev

How to get the summation of days in specific month of year in range

From Dev

Get the last weekday of a month

From Dev

get number of working days in specific month

From Dev

get number of working days in specific month

From Dev

Get all days and date for a given month

From Dev

get all days and dates for the selected month of a year

From Dev

How to get the weekday from day of month using pyspark

From Dev

How to get the DATE, by supplying year, month, week number, weekday in excel

From Dev

How to get the DATE, by supplying year, month, week number, weekday in excel

From Dev

PHP: get weekday number of the month

From Dev

How to get number of days in a month excluding weekends

From Dev

Get all the day sums between two days inside their month object

From Dev

Get weekday and date of the following days in Android

From Dev

How to list how many days for a month for a specific year?

From Dev

How to get rid of all "weekday words" from a string

From Dev

How to get rid of all "weekday words" from a string

From Dev

How to get the all mondays in a month?

From Dev

How to get the all mondays in a month?

From Dev

Get a given weekday in a given month with JavaScript

From Dev

How to get a weekday in Globalize?

From Dev

How to select the Average per WORK Day/Week/Month/60 Days/90 Days when not all days are in table?

From Dev

How to select the Average per WORK Day/Week/Month/60 Days/90 Days when not all days are in table?

From Dev

MySQL - How to show all days records in particular month?

From Dev

How to display all the days of a month in SQL Server 2008?

From Dev

how pivot the days of a month

From Dev

SQL query for all the days of a month

From Dev

given certain date, how to get the first day of previous month with the same weekday in mysql

Related Related

  1. 1

    How to get all days in current month?

  2. 2

    How to get the number of days in a specific month using Java Calendar?

  3. 3

    How to get the summation of days in specific month of year in range

  4. 4

    Get the last weekday of a month

  5. 5

    get number of working days in specific month

  6. 6

    get number of working days in specific month

  7. 7

    Get all days and date for a given month

  8. 8

    get all days and dates for the selected month of a year

  9. 9

    How to get the weekday from day of month using pyspark

  10. 10

    How to get the DATE, by supplying year, month, week number, weekday in excel

  11. 11

    How to get the DATE, by supplying year, month, week number, weekday in excel

  12. 12

    PHP: get weekday number of the month

  13. 13

    How to get number of days in a month excluding weekends

  14. 14

    Get all the day sums between two days inside their month object

  15. 15

    Get weekday and date of the following days in Android

  16. 16

    How to list how many days for a month for a specific year?

  17. 17

    How to get rid of all "weekday words" from a string

  18. 18

    How to get rid of all "weekday words" from a string

  19. 19

    How to get the all mondays in a month?

  20. 20

    How to get the all mondays in a month?

  21. 21

    Get a given weekday in a given month with JavaScript

  22. 22

    How to get a weekday in Globalize?

  23. 23

    How to select the Average per WORK Day/Week/Month/60 Days/90 Days when not all days are in table?

  24. 24

    How to select the Average per WORK Day/Week/Month/60 Days/90 Days when not all days are in table?

  25. 25

    MySQL - How to show all days records in particular month?

  26. 26

    How to display all the days of a month in SQL Server 2008?

  27. 27

    how pivot the days of a month

  28. 28

    SQL query for all the days of a month

  29. 29

    given certain date, how to get the first day of previous month with the same weekday in mysql

HotTag

Archive