游戏技术文章

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

时间:2017-2-6 12:51:30  作者:棋牌资源网  来源:棋牌资源网  查看:7715  评论:0
内容摘要:介绍三种方法获取 Date (NSDate) 的年月日。用 date 表示当前日期。测试日期为公历 2017 年 2 月 5 日,农历丁酉年,鸡年,正月初九。let date: Date = Date()NSDate *date = [NSDate date];获取公历年月日用 ...
介绍三种方法获取 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_获取公历、农历日期的年月日

标签:iOS获取公历农历日期的年月日 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved