NSCalendar的使用(一)對象創(chuàng)建以及屬性解釋

NSCalendar類,主要用來確定日歷單位(比如年代、年份和工作日)和絕對時(shí)間點(diǎn)之間的關(guān)系,并且提供計(jì)算和比較日期的功能

1.NSCalendarIdentifier

NSCalendarIdentifier是日歷類型的標(biāo)識符,常用在創(chuàng)建或設(shè)置日歷類型的時(shí)候,包含了公歷、農(nóng)歷、佛歷等等類型

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日歷的單位,比如年、月、日、小時(shí)等,常用與獲取日期中的指定信息。

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 時(shí)代,我們使用公歷,所以只有公元前BC和公元AD,對應(yīng)0和1,其他日歷可能會有更多,因?yàn)楹芏嗳諝v會根據(jù)國王等重新紀(jì)年
  • NSCalendarUnitYear
  • NSCalendarUnitMonth
  • NSCalendarUnitDay
  • NSCalendarUnitHour 小時(shí)
  • NSCalendarUnitMinute 分鐘
  • NSCalendarUnitSecond
  • NSCalendarUnitWeekday 一周的第幾天,星期日為1
  • NSCalendarUnitWeekdayOrdinal 今天是這周的第幾個(gè)周幾,比如當(dāng)前日期是周3,獲取到的NSCalendarUnitWeekdayOrdinal是2的話,就說明這是本月第2個(gè)周3
  • NSCalendarUnitQuarter 刻鐘
  • NSCalendarUnitWeekOfMonth 本月的第幾周
  • NSCalendarUnitWeekOfYear 本年的第幾周
  • NSCalendarUnitYearForWeekOfYear 第幾年,和我們正常的年有些區(qū)別,我們正常的一年是365或者366,是沒辦法被7整除的,會出現(xiàn)有的周跨年了,然后ISO 8601將年根據(jù)整周倆設(shè)計(jì)了一個(gè)年份,也就是52周(364天)或者53周(371天)為一年,這里獲取到的就是以這種方式來設(shè)置的年份,一般情況下我們不會使用到
  • NSCalendarUnitNanosecond 納秒
  • NSCalendarUnitCalendar 額····,貌似拿回來一個(gè)NSCFCalendar,沒有去看細(xì)節(jié)
  • NSCalendarUnitTimeZone 時(shí)區(qū)

3.NSCalendarOptions

NSCalendarOptions用于指定涉及日歷的算術(shù)操作選項(xiàng)

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的創(chuàng)建方法

NSCalendar中對象的創(chuàng)建類方法,如下

@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的差異

currentCalendarautoupdatingCurrentCalendar都是以屬性的方式出現(xiàn),但是使用了關(guān)鍵字class,因此我們將其當(dāng)做類方法來使用。

兩者都是獲取日歷信息,日歷信息的格式是以用戶系統(tǒng)的首選日歷設(shè)置來決定的,比如我們系統(tǒng)設(shè)置的是公歷,那么獲取到的日期信息就是公歷的日期格式。

區(qū)別在于,currentCalendarautoupdatingCurrentCalendar獲取到對象后,如果去系統(tǒng)設(shè)置中將公歷改成佛歷,繼續(xù)使用這個(gè)對象,currentCalendar還是公歷的日期,但是autoupdatingCurrentCalendar顯示的是佛歷的日期。

4.2 +calendarWithIdentifier:

+ (nullable NSCalendar *)calendarWithIdentifier:(NSCalendarIdentifier)calendarIdentifierConstant是通過指定日歷的類型來獲取日歷。

4.3 - init

標(biāo)注了API_UNAVAILABLE(macos, ios, watchos, tvos),這個(gè)是無法使用了。

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<NSString *> *eraSymbols      API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *longEraSymbols  API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));

@property (readonly, copy) NSArray<NSString *> *monthSymbols                    API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *shortMonthSymbols               API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *veryShortMonthSymbols           API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *standaloneMonthSymbols          API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *shortStandaloneMonthSymbols     API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *veryShortStandaloneMonthSymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));

@property (readonly, copy) NSArray<NSString *> *weekdaySymbols                    API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *shortWeekdaySymbols               API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *veryShortWeekdaySymbols           API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *standaloneWeekdaySymbols          API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *shortStandaloneWeekdaySymbols     API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *veryShortStandaloneWeekdaySymbols API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));

@property (readonly, copy) NSArray<NSString *> *quarterSymbols                 API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *shortQuarterSymbols            API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *standaloneQuarterSymbols       API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSArray<NSString *> *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國家和地區(qū)信息
  • timeZone 時(shí)區(qū)信息
  • firstWeekday設(shè)置每周的第一為星期幾,默認(rèn)指為1代表星期天,按中國的習(xí)慣需要設(shè)置為2代表星期一為每個(gè)星期的第一天,這個(gè)設(shè)置會影響計(jì)算本周是第幾周。

2022年4月日歷截圖.png

上圖的4月17是周日,如果設(shè)置周日為每周的起始日,那目前就是4月的第3周,如果設(shè)置周一為每周的起始日,那么現(xiàn)在還是第2周
ps:完整的一周才算,所以4月1日和4月2日所在的那周不是第一周,如果想修改這個(gè)設(shè)置,需要使用后面的minimumDaysInFirstWeek

  • minimumDaysInFirstWeek設(shè)置一周的最小天數(shù),說起來有點(diǎn)拗口不好理解,請看下圖
2022年4月日歷截圖.png

還是2022年4月17日,minimumDaysInFirstWeek系統(tǒng)默認(rèn)值7,也就是滿7天的周才會被計(jì)算在本年或者本月的周數(shù)里,根據(jù)默認(rèn)的設(shè)置來,4月17日所在的周數(shù)是4月的第三周(此次默認(rèn)firstWeekday為1),因?yàn)?月1日和4月2日所在的那周不夠7天不會被算作第一周,如果我們將minimumDaysInFirstWeek設(shè)置為2,那么4月17日所在的周數(shù)就是4月的第四周了。

  • 其他的Symbols
    這些Symbols的數(shù)組其實(shí)就是一些符號的數(shù)組,比如月份的數(shù)組monthSymbols如下
(
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容