欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-12 20:50:34
介绍三种方法获取 date (nsdate) 的年月日。 用 date 表示当前日期。测试日期为公历 2017 年 2 月 5 日,农历丁酉年,鸡年,正月初九。...

介绍三种方法获取 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]);

结果

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

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

结果

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

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

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

获取农历年月日

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

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

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

结果

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

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

结果

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

计算日期年份的生肖

自定义一个类 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))

结果

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

计算日期年份的天干地支

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

结果

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

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