Make a new NSDate by combining two NSDates

前端 未结 5 1749
故里飘歌
故里飘歌 2021-02-15 11:11

I have NSDate* day1 and NSDate* day2. How can I make a NSDate* day3 by copying only year/month/day from day1 and hour/minute/second from d

5条回答
  •  盖世英雄少女心
    2021-02-15 11:36

    You need to create an NSCalendar and then get the date components from the first and second dates and then get the date using the new date components from the NSCalendar.

    Use the NSCalendar methods

    – components:fromDate:
    – dateFromComponents:
    

    and the corresponding unit flags

    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit; 
    

提交回复
热议问题