将gregorian日期字符串转换为伊斯兰日期可以得到正确的&不正确的结果 [英] Converting a gregorian date string to Islamic date gives correct & incorrect results

查看:169
本文介绍了将gregorian日期字符串转换为伊斯兰日期可以得到正确的&不正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个日期字符串:(1) 24/04/2013 和(2) 19/03/2013 我要转换这些日期到伊斯兰(Um Al Qura)日期,我使用这个代码块:

  NSDateFormatter * df = [[ NSDateFormatter alloc] init]; 
df.dateFormat = @dd / MM / yyyy;
df.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate * dateInGrogrian = [df dateFromString:@24/04/2013​​];

NSDateFormatter * df2 = [[NSDateFormatter alloc] init];
NSCalendar * cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
[df2 setCalendar:cal];
[df2 setDateFormat:@dd / MM / yyyy];
NSLog(@Converted date to Islamic =%@,[df2 stringFromDate:dateInGrogrian]);

如果输入字符串是24/04/2013 NSLog 显示:



将日期转换为伊斯兰= 14/06/1434 在所有伊斯兰国家使用的正式伊斯兰日历)。但是如果输入字符串是19/03/2013,则显示 NSLog



日期到伊斯兰= 08/05/1434 这是不正确的(根据伊斯兰日历,正确的日期必须是07/05/1434,这是1天后)。



- 在您建议回答之前需要考虑的注意事项:



(1)尝试使用日历标识符 NSIslamicCivilCalendar 而不是 NSIslamicCalendar ,但无效:转换的日期之一是正确的,

(2)我已尝试使用GMT时区,如下所示: [df2 setTimeZone:[NSTimeZone timeZoneWithName:@GMT]]; ,这产生了第二天的正确转换日期,但不正确的第一天/ p>

(3)我尝试过组合使用解决方案: NSIslamicCivilCalendar 区域, NSIslamicCalendar 有/无GMT时区,但也无效...



代码块满足这两个日期?所以我确保任何提供的gregorian日期字符串正确转换为伊斯兰日期字符串。



非常感谢你。

解决方案

在打印出过去4年的所有日期后,似乎苹果使用一个有点被接受的公式来数学计算伊斯兰日历,因为正如Moxy指出的,实际的日历不是基于科学日子,而是月球近似。



这个公式是所有奇数月份都有30天,所有偶数月份都有29天,在任何一年的最后一个月增加一天,年mod 30是以下之一:2,5,7,10,13,16,18,21,24,26或29.



平均月长29.53056天,与农历月相当接近29.53059天。



此公式已在计算机中的其他位置使用,因为它是计算日期的唯一方法,而不使用历史中每年的查找表。因此,除非你自己写查找表,这将是准确的,你可以得到所有的日期向前和向前。


I have the following two date strings: (1) 24/04/2013 and (2) 19/03/2013 I'm trying to convert these dates into Islamic (Um Al Qura) dates, I'm using this code block to do so:

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"dd/MM/yyyy";
    df.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateInGrogrian = [df dateFromString:@"24/04/2013"];

    NSDateFormatter *df2 = [[NSDateFormatter alloc] init];
    NSCalendar * cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
    [df2 setCalendar:cal];
    [df2 setDateFormat:@"dd/MM/yyyy"];
    NSLog(@"Converted date to Islamic = %@",[df2 stringFromDate:dateInGrogrian]); 

if the input string was 24/04/2013 the NSLog displayed:

Converted date to Islamic = 14/06/1434 which is correct (according to the formal Islamic calendar which is used in all Islamic countries). But if the input string was 19/03/2013 the NSLog displayed:

Converted date to Islamic = 08/05/1434 which is incorrect (according to the Islamic calendar the correct date must be 07/05/1434 which's 1 day behind).

-Notes to consider before you suggest an answer:

(1) I have tried to use the calendar identifier NSIslamicCivilCalendar instead of NSIslamicCalendar , but to no avail: one of the converted dates was correct and the other was wrong (1 day behind).

(2) I have tried to use GMT time zone like this: [df2 setTimeZone : [NSTimeZone timeZoneWithName:@"GMT"]]; , this produced a correct converted date for day (2) but incorrect for day (1) (1 day behind).

(3) I have tried combinations of solutions: NSIslamicCivilCalendar with / without GMT time zone, NSIslamicCalendar with / without GMT time Zone, but also to no avail ...

can anybody provide a code block that satisfies both dates ? so I ensure that any provided gregorian date string is correctly converted into Islamic date string.

thank you so much.

解决方案

After printing out all the dates from the last 4 years it seems that Apple uses a somewhat accepted formula for mathematically calculating the Islamic Calendar, since as Moxy pointed out, the actual calendar is not based on scientific days but instead a lunar approximation.

This formula is that all odd numbered months have 30 days and all even numbered months have 29 days with an extra day added to the last month in any year in which the number year mod 30 is one of the following: 2, 5, 7, 10, 13, 16, 18, 21, 24, 26, or 29.

The result is an average month length of 29.53056 days, which is quite close to the lunar month of 29.53059 days.

This formula has been used elsewhere in computers, as it is the only method to calculate the date without using a lookup table for every year in history. As such unless you write the lookup table yourself, this will be as accurate as you can get for all dates going backwards and forwards.

这篇关于将gregorian日期字符串转换为伊斯兰日期可以得到正确的&不正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆