【问题标题】:How to convert buddhist Date To Gregorian Date Properly iOS如何正确地将佛教日期转换为公历iOS
【发布时间】:2014-02-17 20:29:17
【问题描述】:

如何将 2557 年 1 月 27 日 佛教日期转换为 2013 年 1 月 27 日

是的,我知道如果我从 2557 中减去 543,我将得到 2014。但是,我希望它使用 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];

谢谢。

【问题讨论】:

  • 我假设您将格式化程序的日历设置为 Buddist,然后读取日期。一旦正确读取为 NSDate,您就可以根据需要对其进行格式化。
  • 检查这个 stackoverflow.com/a/24417064/977910

标签: ios nsdate


【解决方案1】:

创建公历实例,然后使用 NSDateFormatter 对其进行格式化

let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let dateFormatter = NSDateFormatter()
dateFormatter.calendar = gregorianCalendar
dateFormatter.dateFormat = "dd/MM/yyyy HH:mm:ss"

print(dateFormatter.stringFromDate(NSDate()))

【讨论】:

    【解决方案2】:

    这几乎是来自 Apple 文档的复制/粘贴,只是稍作更改以使用正确的日历类型。我将日期硬编码为您在示例中使用的值。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2023-03-30
      • 2023-03-20
      • 1970-01-01
      • 2013-07-21
      相关资源
      最近更新 更多