How to get Monday's date of the current week in swift

前端 未结 8 1752
死守一世寂寞
死守一世寂寞 2020-11-29 05:36

I\'m trying to get Monday\'s date of the current week. This is treated as the first day of the week in my table view. I also need to get Sunday\'s of the current week. This

8条回答
  •  孤独总比滥情好
    2020-11-29 06:00

    Try to use:

    calendar.firstWeekday = 2
    

    Edit

    To be more specific: NSCalendar.currentCalendar() returns user calendar. According to docs:

    The returned calendar is formed from the settings for the current user’s chosen system locale overlaid with any custom settings the user has specified in System Preferences.

    If you want always Monday as first day, I think you should use:

    let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    calendar!.firstWeekday = 2
    

提交回复
热议问题