如何正确地将佛教日期转换为公历日期 iOS [英] How to convert buddhist Date To Gregorian Date Properly iOS
本文介绍了如何正确地将佛教日期转换为公历日期 iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 2557 年 1 月 27 日 的佛教日期转换为 2013 年 1 月 27 日.
How to convert Jan 27, 2557 a Buddhist date into Jan 27, 2013.
是的,我知道如果我从 2557 中减去 543,我会得到 2014.但是,我希望它使用 NSDateFormatter 来解决.
Yes, I'm aware that If I subtract 543 from 2557 I'll get 2014. However, I want it to solve using NSDateFormatter.
这是我的代码:
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
TimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSS'Z'"];
// Buddhist date should be converted to Gregorian date. But the
// Date still in Buddhist form
NSDate *gregDate = [dateFormatter dateFromString:buddhistDate];
谢谢.
推荐答案
这几乎是从 Apple 文档中复制/粘贴的,只是稍作改动以使用正确的日历类型.我将日期硬编码为您在示例中使用的值.
This is pretty much copy/paste from the Apple docs, with a minor change to use the correct calendar types. I'm hardcoding the date to the values you used in your example.
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:27];
[comps setMonth:1];
[comps setYear:2557];
NSCalendar *buddhist = [[NSCalendar alloc]
initWithCalendarIdentifier:NSBuddhistCalendar];
NSDate *date = [buddhist dateFromComponents:comps];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit |
NSYearCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:date];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
这篇关于如何正确地将佛教日期转换为公历日期 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文