iOS 获取公历、农历日期的年月日的实例代码

时间:2021-09-29 09:47:45

介绍三种方法获取 date (nsdate) 的年月日。

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

?
1
2
3
let date: date = date()
 
nsdate *date = [nsdate date];

获取公历年月日

用 calendar (nscalendar) 获取公历年月日

?
1
2
3
4
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))
?
1
2
3
4
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]);

结果

iOS 获取公历、农历日期的年月日的实例代码

用 calendar 和 datecomponents (nscalendar 和 nsdatecomponents) 获取公历年月日

?
1
2
3
4
5
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!)
?
1
2
3
4
5
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);

结果

iOS 获取公历、农历日期的年月日的实例代码

用 dateformatter (nsdateformatter) 获取公历年月日

?
1
2
3
4
5
6
7
8
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))
?
1
2
3
4
5
6
7
8
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]);

iOS 获取公历、农历日期的年月日的实例代码

获取农历年月日

用 calendar (nscalendar) 获取农历年月日

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

?
1
let calendar: calendar = calendar(identifier: .chinese)
?
1
nscalendar *calendar = [nscalendar calendarwithidentifier:nscalendaridentifierchinese];

结果

iOS 获取公历、农历日期的年月日的实例代码

用 calendar 和 datecomponents (nscalendar 和 nsdatecomponents) 获取农历年月日

同上节用 calendar (nscalendar) 获取农历年月日

用 dateformatter (nsdateformatter) 获取农历年月日

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

?
1
2
let formatter: dateformatter = dateformatter()
formatter.calendar = calendar(identifier: .chinese)
?
1
2
nsdateformatter *formatter = [[nsdateformatter alloc] init];
formatter.calendar = [nscalendar calendarwithidentifier:nscalendaridentifierchinese];

结果

iOS 获取公历、农历日期的年月日的实例代码

计算日期年份的生肖

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

 

复制代码 代码如下:

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

 

十二生肖数组

?
1
2
3
4
5
6
7
8
9
10
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))
}

测试

?
1
print("chinese zodiac string:", chinesecalendar.zodiac(withdate: date))

结果

iOS 获取公历、农历日期的年月日的实例代码

计算日期年份的天干地支

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

天干地支数组

?
1
2
private let heavenlystems: [string] = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
private let earthlybranches: [string] = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]

chinesecalendar 的类方法

?
1
2
3
4
5
6
7
8
9
10
11
12
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))
}

测试

?
1
print("chinese era string:", chinesecalendar.era(withdate: date))

结果

iOS 获取公历、农历日期的年月日的实例代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/silence-cnblogs/archive/2017/02/05/6368437.html