Working with NSDate components in Swift

后端 未结 4 463
梦毁少年i
梦毁少年i 2021-02-08 04:19

I have a date that I need to split in some components. for example

let components = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour
let date = c         


        
4条回答
  •  礼貌的吻别
    2021-02-08 04:28

    You're going to need to make another variable called "calendar" (NSCalendar object) and using the init "currentCalendar" method. Once you have the calendar object defined, call the method under calendar rangeOfUnit:NSWeekCalendarUnit inUnit:NSMonthCalendarUnit forDate:date. Then make a NSInteger called amountOfWeeks equal to the range the above function returns. The length of the range the function returns will be the number of weeks in that month.

提交回复
热议问题