服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS 获取公历、农历日期的年月日的实例代码

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

2021-03-03 16:02Silence_cnblogs IOS

本篇文章主要介绍了iOS 获取公历、农历日期的年月日的实例代码,主要介绍了三种方法,具有一定的参考价值,有兴趣的可以了解一下。

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

延伸 · 阅读

精彩推荐
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01