iOS 获取公历、农历日期的年月日

介绍三种方法获取 Date (NSDate) 的年月日。

用 date 表示当前日期。测试日期为公历 2017 年 2 月 5 日,农历丁酉年,鸡年,正月初九。

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]);

结果

用 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);

结果

用 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]);

获取农历年月日

用 Calendar (NSCalendar) 获取农历年月日

与公历相似,更改 Calendar (NSCalendar) 的初始化即可,其他代码相同

let calendar: Calendar = Calendar(identifier: .chinese)
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];

结果

用 Calendar 和 DateComponents (NSCalendar 和 NSDateComponents) 获取农历年月日

同上节用 Calendar (NSCalendar) 获取农历年月日

用 DateFormatter (NSDateFormatter) 获取农历年月日

与公历相似,在初始化 DateFormatter (NSDateFormatter) 之后,给 calendar 属性赋值即可,其他代码相同

let formatter: DateFormatter = DateFormatter()
formatter.calendar = Calendar(identifier: .chinese)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];

结果

计算日期年份的生肖

自定义一个类 ChineseCalendar 来计算。十二生肖数组写在类外面。

十二生肖数组

private let Zodiacs: [String] = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]

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))
}

测试

print("Chinese zodiac string:", ChineseCalendar.zodiac(withDate: date))

结果

计算日期年份的天干地支

在 ChineseCalendar 中用类方法计算。天干地支数组写在类外面。

天干地支数组

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))
}

测试

print("Chinese era string:", ChineseCalendar.era(withDate: date))

结果

转载请注明出处:http://www.cnblogs.com/silence-cnblogs/p/6368437.html

iOS 获取公历、农历日期的年月日的更多相关文章

  1. iOS获取时间、日期

    //获取当前时间 NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease]; [formatter setLoc ...

  2. iOS - 获取当前时间日期星期几

    //获取当前时间日期星期 - (NSString *)getCurrentTimeAndWeekDay { NSArray * arrWeek=[NSArray arrayWithObjects:@& ...

  3. mysql获取表中日期的年月日时分秒

    SELECT year(callTheRollTime) from schedule_account 获取年 SELECT month(callTheRollTime) from schedule_a ...

  4. iOS - 获取系统时间年月日,阳历(公历)日期转农历的方法

    //获取当前时间 NSDate *now = [NSDate date]; NSLog(@" now date is: %@ ",now); NSCalendar *calenda ...

  5. C# 获取农历日期

    //C# 获取农历日期 ///<summary> /// 实例化一个 ChineseLunisolarCalendar ///</summary> private static ...

  6. js 根据年月获取当月有多少天_js获取农历日期_及Js其它常用有用函数

    //根据年月获取当月有多少天 function getDaysInMonth(year, month) { debugger; //parseInt(number,type)这个函数后面如果不跟第2个 ...

  7. Swift3.0 iOS获取当前时间 - 年月日时分秒星期

    Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...

  8. java 获取两个日期之间的所有日期(年月日)

    前言:直接上代码 java 获取两个日期之间的所有日期(年月日) /** * 获取两个日期之间的日期,包括开始结束日期 * @param start 开始日期 * @param end 结束日期 * ...

  9. 利用Javascript获取当前日期的农历日期

    来源:http://www.ido321.com/926.html JavaScript代码 1: /*设置农历日期*/ 2: var CalendarData=new Array(100); 3: ...

随机推荐

  1. H5手机开发锁定表头和首列(惯性滚动)解决方案

    前端时间移动端在做表格的时候需要这个功能,由于还有实现类似原生的惯性滚动功能,于是使用了iscroll插件. iscroll插件下载地址:iscroll5 该功能demo github地址: http ...

  2. compass scss blueprint

    [转载] 今天在执行compass create my-grid –using blueprint 命令时发现报错 google了一下,说是新版compass已经不包括compass-bluprint ...

  3. Zipf’s Law

    Let f(w) be the frequency of a word w in free text. Suppose that all the words of a text are ranked ...

  4. function $(id){ return document.getElementById(id); }导致所有的js不能用解决办法。。。。

    function $(id){ return document.getElementById(id); } document.getElementById(id) 是获得id这个元素的. 相当于定义了 ...

  5. Photoshop安装

    作者:郑超 参考地址:http://bbs.weiphone.com/read-htm-tid-4594713.html 下载地址:http://www.adobe.com/downloads.htm ...

  6. leetcode--012 single number I

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0MAAADGCAIAAACfN8xOAAAZ6UlEQVR4nO3dsZKcurbG8fNOnd1nIv

  7. dom4j生成和解析xml文件

    dom4j生成和解析xml文件 要生成和解析如下格式的xml文件: <?xml version="1.0" encoding="UTF-8"?> & ...

  8. UVa 11747 - Heavy Cycle Edges

    题目大意:计算最小生成树有两种算法:一种是kruskal算法,另一种是与之相反的:如果图中存在环,去掉权重最大的边,直到不存在环.输出去掉的那些边. 可以用kruskal算法解决,在判断一条边时如果加 ...

  9. Bzoj3473

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3473 题解:待更 代码:待更 开始时间: 结束时间:

  10. 第十八篇、OC_使用OAStackView FDStackView (第三方框架)可以使用在ios 7及以上的版本

    UILabel *view1 = [[UILabel alloc]init]; view1.text = @"Label1"; view1.backgroundColor = [U ...