Difference between two NSDate objects — Result also a NSDate

后端 未结 5 1025
礼貌的吻别
礼貌的吻别 2020-11-30 20:38

I have two NSDate objects and I want the difference between the two and the result should again be a NSDate object. Any idea how to achieve this?

Here, I am trying t

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 20:58

    If you subtract 12/12/2001 from 05/05/2002 what will be the date? The chronological distance between two dates can't be a date, it's alway some kind of interval. You can use timeIntervalSinceDate: to calculate the interval.

    To localize you can try the following steps:

    • You can use the NSCalendar with dateFromComponents: passing in a NSDateComponents.

    • To break down a timeInterval into NSDateComponents look at How do I break down an NSTimeInterval into year, months, days, hours, minutes and seconds on iPhone?.

    • Finally use the NSDateFormatter and initWithDateFormat:allowNaturalLanguage: to get your localized string. The Date Format String Syntax shows the different placeholders.

提交回复
热议问题