iOS 获取公历、农历日期的年月日
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 获取公历、农历日期的年月日的更多相关文章
- iOS获取时间、日期
//获取当前时间 NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease]; [formatter setLoc ...
- iOS - 获取当前时间日期星期几
//获取当前时间日期星期 - (NSString *)getCurrentTimeAndWeekDay { NSArray * arrWeek=[NSArray arrayWithObjects:@& ...
- mysql获取表中日期的年月日时分秒
SELECT year(callTheRollTime) from schedule_account 获取年 SELECT month(callTheRollTime) from schedule_a ...
- iOS - 获取系统时间年月日,阳历(公历)日期转农历的方法
//获取当前时间 NSDate *now = [NSDate date]; NSLog(@" now date is: %@ ",now); NSCalendar *calenda ...
- C# 获取农历日期
//C# 获取农历日期 ///<summary> /// 实例化一个 ChineseLunisolarCalendar ///</summary> private static ...
- js 根据年月获取当月有多少天_js获取农历日期_及Js其它常用有用函数
//根据年月获取当月有多少天 function getDaysInMonth(year, month) { debugger; //parseInt(number,type)这个函数后面如果不跟第2个 ...
- Swift3.0 iOS获取当前时间 - 年月日时分秒星期
Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...
- java 获取两个日期之间的所有日期(年月日)
前言:直接上代码 java 获取两个日期之间的所有日期(年月日) /** * 获取两个日期之间的日期,包括开始结束日期 * @param start 开始日期 * @param end 结束日期 * ...
- 利用Javascript获取当前日期的农历日期
来源:http://www.ido321.com/926.html JavaScript代码 1: /*设置农历日期*/ 2: var CalendarData=new Array(100); 3: ...
随机推荐
- jquery.elevateZoom实现仿淘宝看图片,一张小的,一张大用于鼠标经过时候显示
实现这个效果你需要准备两张图片,一张小的,一张大用于鼠标经过时候显示.然后我们只要为img标签添加data-zoom-image属性,其值为大图的地址,最后在javascript中选择该图片调用ele ...
- 滚动时div的背景图片随之滚动
在浏览一些网站时发现有一种效果是当滚动时看到某一DIV的背景也会随之滚动,如下: 当滚动时内容位置保持不变,但是内容后面的背景却在随着滚动.随之我通过审查元素看到了其是通过background-pos ...
- cron 执行php文件
php执行的命令要写全路径,不然无法执行
- ZOJ 3331 Process the Tasks
双塔DP. #include<cstdio> #include<cstring> #include<queue> #include<string> #i ...
- 企业证书APP发布流程 分类: ios相关 app相关 2015-06-10 11:01 212人阅读 评论(0) 收藏
企业发布app的 过程比app store 发布的简单多了,没那么多的要求,哈 但是整个工程的要求还是一样,比如各种像素的icon啊 命名规范啊等等. 下面是具体的流程 1.修改你的 bundle i ...
- Linux怎样访问Windows共享文件和文件夹
常常使用Windows的人可能会发现,Windows计算机之前共享资料非常方便,但是有时候想玩玩Linux的时候,如Fedora.Ubuntu.CentOS等,该怎样才能访问Windows计算机上的文 ...
- leetcode-004 insertion sort list
package leetcode; class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } ...
- Scott用户的四张表:
Scott用户的四张表: 转载:http://www.cnblogs.com/mchina/archive/2012/09/06/2649951.html 在Oracle的学习之中,重点使用的是SQL ...
- Node.js timer的优化故事
前几天nodejs发布了新版本4.0,其中涉及到一个更新比较多的模块,那就是下面要介绍的timer模块. timers: Improved timer performance from porting ...
- Python3基础 map 与 lambda表达式配合 将指定系列元素乘2
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...