iOS 獲取公歷、農(nóng)歷日期的年月日的實(shí)例代碼
介紹三種方法獲取 Date (NSDate) 的年月日。
用 date 表示當(dāng)前日期。測(cè)試日期為公歷 2017 年 2 月 5 日,農(nóng)歷丁酉年,雞年,正月初九。
let date: Date = Date() NSDate *date = [NSDate date];
獲取公歷年月日
用 Calendar (NSCalendar) 獲取公歷年月日
let calendar: Calendar = Calendar(identifier: .gregorian) print("Year:", calendar.component(.year, from: date)) print("Month:", calendar.component(.month, from: date)) print("Day:", calendar.component(.day, from: date))
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]; NSLog(@"Year: %ld", [calendar component:NSCalendarUnitYear fromDate:date]); NSLog(@"Month: %ld", [calendar component:NSCalendarUnitMonth fromDate:date]); NSLog(@"Day: %ld", [calendar component:NSCalendarUnitDay fromDate:date]);
結(jié)果
用 Calendar 和 DateComponents (NSCalendar 和 NSDateComponents) 獲取公歷年月日
let componentSet: Set<Calendar.Component> = Set(arrayLiteral: .year, .month, .day) let components: DateComponents = calendar.dateComponents(componentSet, from: date) print("Year:", components.year!) print("Month:", components.month!) print("Day:", components.day!)
NSCalendarUnit calenderUnit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSDateComponents *components = [calendar components:calenderUnit fromDate:date]; NSLog(@"Year: %ld", components.year); NSLog(@"Month: %ld", components.month); NSLog(@"Day: %ld", components.day);
結(jié)果
用 DateFormatter (NSDateFormatter) 獲取公歷年月日
let formatter: DateFormatter = DateFormatter() print("Date formatter identifier:", formatter.calendar.identifier) // gregorian by default formatter.dateFormat = "y" print("Year:", formatter.string(from: date)) formatter.dateFormat = "M" print("Month:", formatter.string(from: date)) formatter.dateFormat = "d" print("Day:", formatter.string(from: date))
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSLog(@"Date formatter calendar: %@", formatter.calendar.calendarIdentifier); // gregorian by default formatter.dateFormat = @"y"; NSLog(@"Year: %@", [formatter stringFromDate:date]); formatter.dateFormat = @"M"; NSLog(@"Month: %@", [formatter stringFromDate:date]); formatter.dateFormat = @"d"; NSLog(@"Day: %@", [formatter stringFromDate:date]);
獲取農(nóng)歷年月日
用 Calendar (NSCalendar) 獲取農(nóng)歷年月日
與公歷相似,更改 Calendar (NSCalendar) 的初始化即可,其他代碼相同
let calendar: Calendar = Calendar(identifier: .chinese)
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];
結(jié)果
用 Calendar 和 DateComponents (NSCalendar 和 NSDateComponents) 獲取農(nóng)歷年月日
同上節(jié)用 Calendar (NSCalendar) 獲取農(nóng)歷年月日
用 DateFormatter (NSDateFormatter) 獲取農(nóng)歷年月日
與公歷相似,在初始化 DateFormatter (NSDateFormatter) 之后,給 calendar 屬性賦值即可,其他代碼相同
let formatter: DateFormatter = DateFormatter() formatter.calendar = Calendar(identifier: .chinese)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];
結(jié)果
計(jì)算日期年份的生肖
自定義一個(gè)類 ChineseCalendar 來(lái)計(jì)算。十二生肖數(shù)組寫在類外面。
private let Zodiacs: [String] = ["鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬"]
十二生肖數(shù)組
ChineseCalendar 的類方法 static func zodiac(withYear year: Int) -> String { let zodiacIndex: Int = (year - 1) % Zodiacs.count return Zodiacs[zodiacIndex] } static func zodiac(withDate date: Date) -> String { let calendar: Calendar = Calendar(identifier: .chinese) return zodiac(withYear: calendar.component(.year, from: date)) }
測(cè)試
print("Chinese zodiac string:", ChineseCalendar.zodiac(withDate: date))
結(jié)果
計(jì)算日期年份的天干地支
在 ChineseCalendar 中用類方法計(jì)算。天干地支數(shù)組寫在類外面。
天干地支數(shù)組
private let HeavenlyStems: [String] = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"] private let EarthlyBranches: [String] = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
ChineseCalendar 的類方法
static func era(withYear year: Int) -> String { let heavenlyStemIndex: Int = (year - 1) % HeavenlyStems.count let heavenlyStem: String = HeavenlyStems[heavenlyStemIndex] let earthlyBrancheIndex: Int = (year - 1) % EarthlyBranches.count let earthlyBranche: String = EarthlyBranches[earthlyBrancheIndex] return heavenlyStem + earthlyBranche } static func era(withDate date: Date) -> String { let calendar: Calendar = Calendar(identifier: .chinese) return era(withYear: calendar.component(.year, from: date)) }
測(cè)試
print("Chinese era string:", ChineseCalendar.era(withDate: date))
結(jié)果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView
這篇文章主要介紹了IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView的相關(guān)資料,需要的朋友可以參考下2017-01-01iOS開(kāi)發(fā)中使用cocos2d添加觸摸事件的方法
這篇文章主要介紹了iOS開(kāi)發(fā)中使用cocos2d添加觸摸事件的方法,cocos2d是制作iOS游戲的利器,需要的朋友可以參考下2015-10-10基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解
下面小編就為大家分享一篇基于iOS Realm數(shù)據(jù)庫(kù)的使用實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01iOS中UITableView Cell實(shí)現(xiàn)自定義單選功能
本篇文章主要介紹了iOS中UITableView Cell實(shí)現(xiàn)自定義單選功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02解決iOS UITextField 編輯時(shí)文本偏移問(wèn)題
這篇文章主要介紹了解決iOS UITextField 編輯時(shí)文本偏移問(wèn)題,需要的朋友可以參考下2017-05-05iOS利用CoreImage實(shí)現(xiàn)人臉識(shí)別詳解
OS的人臉識(shí)別從iOS 5(2011)就有了,不過(guò)一直沒(méi)怎么被關(guān)注過(guò)。人臉識(shí)別API允許開(kāi)發(fā)者不僅可以檢測(cè)人臉,也可以檢測(cè)到面部的一些特殊屬性,比如說(shuō)微笑或眨眼。下面這篇文章主要給大家介紹了iOS利用CoreImage實(shí)現(xiàn)人臉識(shí)別的相關(guān)資料,需要的朋友可以參考下。2017-05-05iOS 輸入驗(yàn)證碼或密碼,自動(dòng)下一位的實(shí)例
下面小編就為大家分享一篇iOS 輸入驗(yàn)證碼或密碼,自動(dòng)下一位的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Objective-C關(guān)鍵字@property使用原理探究
這篇文章主要為大家介紹了Objective-C關(guān)鍵字@property使用原理探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01