IOS时间处理

夹谷奇
2023-12-01

IOS里面关于时间处理的类库有很多:

NSDate、NSCalendar、NSDateComponents、NSDateFormatter、NSTimeZone


NSDate,主要处理时间点

NSCalender,主要用于处理日历

NSDateComponents,主要用于处理日期的计算,比如把某天往前推几年几月几天等

NSDateFormatter,主要用于时间戳的处理

NSTimeZone,主要用于时区的设置


这是官方的说法:

  • NSDate allows you to represent an absolute point in time.

  • NSCalendar allows you to represent a particular calendar, such as a Gregorian or Hebrew calendar. It provides the interface for most date-based calculations and allows you to convert between NSDate objects and NSDateComponents objects.

  • NSDateComponents allows you to represent the components of a particular date, such as hour, minute, day, year, and so on.

In addition to these classes, NSTimeZone allows you to represent a geopolitical region’s time zone information. It eases the task of working across different time zones and performing calculations that may be affected by daylight savings time transitions.


具体区别,文档写的很清楚

Date and Time Programming Guide

 类似资料: