NSDate beginning of day and end of day

后端 未结 23 2056
广开言路
广开言路 2020-11-29 23:28
    -(NSDate *)beginningOfDay:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:( NSMonthCalend         


        
23条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 23:55

    Calendar units should be thought of as intervals. As of iOS 10 Calendar has some nice methods for this

    let day = Calendar.autoupdatingCurrent.dateInterval(of: .day, for: Date())
    day?.end
    day?.start
    

    You can use the same method, to get the start/end of any calendar component (week/month/year etc)

提交回复
热议问题