Initialize a NSDate object with a specific time

后端 未结 5 1898
一整个雨季
一整个雨季 2021-02-14 11:17

I have time in string format for example 2:00. I want to initialize it to NSDate with present date. I tried doin

5条回答
  •  余生分开走
    2021-02-14 11:51

      func initializeDateWithTime(date:NSDate,hrs:Int,minutes:Int) -> NSDate{
        let today = date
         let gregorian:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
         let dateComponents = gregorian.components([.Year, .Month, .Day], fromDate: today)
    
         dateComponents.hour = hrs;
         dateComponents.minute = minute;
    
    
         let todayAtX = gregorian.dateFromComponents(dateComponents)
         return todayAtX!
    }
    

提交回复
热议问题