NSCalendar
类,主要用来确定日历单位(比如年代、年份和工作日)和绝对时间点之间的关系,并且提供计算和比较日期的功能
1.NSCalendarIdentifier
NSCalendarIdentifier
是日历类型的标识符,常用在创建或设置日历类型的时候,包含了公历、农历、佛历等等类型
typedef NSString * NSCalendarIdentifier NS_TYPED_EXTENSIBLE_ENUM;
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierGregorian API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)); // the common calendar in Europe, the Western Hemisphere, and elsewhere
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierBuddhist API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierChinese API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierCoptic API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierEthiopicAmeteMihret API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierEthiopicAmeteAlem API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierHebrew API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierISO8601 API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierIndian API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierIslamic API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierIslamicCivil API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierJapanese API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierPersian API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierRepublicOfChina API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
// A simple tabular Islamic calendar using the astronomical/Thursday epoch of CE 622 July 15
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierIslamicTabular API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
// The Islamic Umm al-Qura calendar used in Saudi Arabia. This is based on astronomical calculation, instead of tabular behavior.
FOUNDATION_EXPORT NSCalendarIdentifier const NSCalendarIdentifierIslamicUmmAlQura API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
2.NSCalendarUnit
NSCalendarUnit
日历的单位,比如年、月、日、小时等,常用与获取日期中的指定信息。
typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) {
NSCalendarUnitEra = kCFCalendarUnitEra,
NSCalendarUnitYear = kCFCalendarUnitYear,
NSCalendarUnitMonth = kCFCalendarUnitMonth,
NSCalendarUnitDay = kCFCalendarUnitDay,
NSCalendarUnitHour = kCFCalendarUnitHour,
NSCalendarUnitMinute = kCFCalendarUnitMinute,
NSCalendarUnitSecond = kCFCalendarUnitSecond,
NSCalendarUnitWeekday = kCFCalendarUnitWeekday,
NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal,
NSCalendarUnitQuarter API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) = kCFCalendarUnitQuarter,
NSCalendarUnitWeekOfMonth API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) = kCFCalendarUnitWeekOfMonth,
NSCalendarUnitWeekOfYear API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) = kCFCalendarUnitWeekOfYear,
NSCalendarUnitYearForWeekOfYear API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) = kCFCalendarUnitYearForWeekOfYear,
NSCalendarUnitNanosecond API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)) = (1 << 15),
NSCalendarUnitCalendar API_AVAILABLE(macos(10.7), ios(4.0), watchos(2.0), tvos(9.0)) = (1 << 20),
NSCalendarUnitTimeZone API_AVAILABLE(macos(10.7), ios(4.0), watchos(2.0), tvos(9.0)) = (1 << 21),
};
-
NSCalendarUnitEra
时代,我们使用公历,所以只有公元前BC和公元AD,对应0和1,其他日历可能会有更多,因为很多日历会根据国王等重新纪年 -
NSCalendarUnitYear
年 -
NSCalendarUnitMonth
月 -
NSCalendarUnitDay
日 -
NSCalendarUnitHour
小时 -
NSCalendarUnitMinute
分钟 -
NSCalendarUnitSecond
秒 -
NSCalendarUnitWeekday
一周的第几天,星期日为1 -
NSCalendarUnitWeekdayOrdinal
今天是这周的第几个周几,比如当前日期是周3,获取到的NSCalendarUnitWeekdayOrdinal
是2的话,就说明这是本月第2个周3 -
NSCalendarUnitQuarter
刻钟 -
NSCalendarUnitWeekOfMonth
本月的第几周 -
NSCalendarUnitWeekOfYear
本年的第几周 -
NSCalendarUnitYearForWeekOfYear
第几年,和我们正常的年有些区别,我们正常的一年是365或者366,是没办法被7整除的,会出现有的周跨年了,然后ISO 8601将年根据整周俩设计了一个年份,也就是52周(364天)或者53周(371天)为一年,这里获取到的就是以这种方式来设置的年份,一般情况下我们不会使用到 -
NSCalendarUnitNanosecond
纳秒 -
NSCalendarUnitCalendar
额····,貌似拿回来一个NSCFCalendar,没有去看细节 -
NSCalendarUnitTimeZone
时区
3.NSCalendarOptions
NSCalendarOptions
用于指定涉及日历的算术操作选项
typedef NS_OPTIONS(NSUInteger, NSCalendarOptions) {
NSCalendarWrapComponents = (1UL << 0), // option for arithmetic operations that do calendar addition
NSCalendarMatchStrictly API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)) = (1ULL << 1),
NSCalendarSearchBackwards API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)) = (1ULL << 2),
NSCalendarMatchPreviousTimePreservingSmallerUnits API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)) = (1ULL << 8),
NSCalendarMatchNextTimePreservingSmallerUnits API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)) = (1ULL << 9),
NSCalendarMatchNextTime API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)) = (1ULL << 10),
NSCalendarMatchFirst API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)) = (1ULL << 12),
NSCalendarMatchLast API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)) = (1ULL << 13)
};
4.NSCalendar的创建方法
NSCalendar
中对象的创建类方法,如下
@property (class, readonly, copy) NSCalendar *currentCalendar;
@property (class, readonly, strong) NSCalendar *autoupdatingCurrentCalendar
+ (nullable NSCalendar *)calendarWithIdentifier:(NSCalendarIdentifier)calendarIdentifierConstant
- (instancetype)init API_UNAVAILABLE(macos, ios, watchos, tvos);
- (nullable id)initWithCalendarIdentifier:(NSCalendarIdentifier)ident NS_DESIGNATED_INITIALIZER;
4.1 currentCalendar和autoupdatingCurrentCalendar的差异
currentCalendar
和autoupdatingCurrentCalendar
都是以属性的方式出现,但是使用了关键字class
,因此我们将其当做类方法来使用。
两者都是获取日历信息,日历信息的格式是以用户系统的首选日历设置来决定的,比如我们系统设置的是公历,那么获取到的日期信息就是公历的日期格式。
区别在于,currentCalendar
和autoupdatingCurrentCalendar
获取到对象后,如果去系统设置中将公历改成佛历,继续使用这个对象,currentCalendar
还是公历的日期,但是autoupdatingCurrentCalendar
显示的是佛历的日期。
4.2 +calendarWithIdentifier:
+ (nullable NSCalendar *)calendarWithIdentifier:(NSCalendarIdentifier)calendarIdentifierConstant
是通过指定日历的类型来获取日历。
4.3 - init
标注了API_UNAVAILABLE(macos, ios, watchos, tvos)
,这个是无法使用了。
4.4 -initWithCalendarIdentifier
- (nullable id)initWithCalendarIdentifier:(NSCalendarIdentifier)ident
的使用与+ (nullable NSCalendar *)calendarWithIdentifier:(NSCalendarIdentifier)calendarIdentifierConstant
一样,都是通过指定日历的类型来获取日历。
5. NSCalendar的属性
@property (readonly, copy) NSCalendarIdentifier calendarIdentifier;
@property (nullable, copy) NSLocale *locale;
@property (copy) NSTimeZone *timeZone;
@property NSUInteger firstWeekday;
@property NSUInteger minimumDaysInFirstWeek;
// Methods to return component name strings localized to the calendar's locale
@property (readonly, copy) NSArray *eraSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *longEraSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *monthSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *shortMonthSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *veryShortMonthSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *standaloneMonthSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *shortStandaloneMonthSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *veryShortStandaloneMonthSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *weekdaySymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *shortWeekdaySymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *veryShortWeekdaySymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *standaloneWeekdaySymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *shortStandaloneWeekdaySymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *veryShortStandaloneWeekdaySymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *quarterSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *shortQuarterSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *standaloneQuarterSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray *shortStandaloneQuarterSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSString *AMSymbol API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSString *PMSymbol API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
-
calendarIdentifier
获取日历的类别 -
locale
国家和地区信息 -
timeZone
时区信息 -
firstWeekday
设置每周的第一为星期几,默认指为1代表星期天,按中国的习惯需要设置为2代表星期一为每个星期的第一天,这个设置会影响计算本周是第几周。
上图的4月17是周日,如果设置周日为每周的起始日,那目前就是4月的第3周,如果设置周一为每周的起始日,那么现在还是第2周
ps:完整的一周才算,所以4月1日和4月2日所在的那周不是第一周,如果想修改这个设置,需要使用后面的minimumDaysInFirstWeek
-
minimumDaysInFirstWeek
设置一周的最小天数,说起来有点拗口不好理解,请看下图
还是2022年4月17日,minimumDaysInFirstWeek
系统默认值7,也就是满7天的周才会被计算在本年或者本月的周数里,根据默认的设置来,4月17日所在的周数是4月的第三周(此次默认firstWeekday为1),因为4月1日和4月2日所在的那周不够7天不会被算作第一周,如果我们将minimumDaysInFirstWeek设置为2,那么4月17日所在的周数就是4月的第四周了。
- 其他的
Symbols
这些Symbols
的数组其实就是一些符号的数组,比如月份的数组monthSymbols
如下
(
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
)