Gregorian in Hebrew

How to convert gregorian date to Hebrew equivalent date? Also, please tell us about these calendars, since I do not know about it.

+3
source share
1 answer

There is a convenient class called there NSCalendar. You create a file like this:

NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendar * hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];

Once you have calendar objects, you can use them to convert dates around to various representations:

NSDate * date = [NSDate date];
NSDateComponents * components = [gregorian components:NSUIntegerMax fromDate:date];
NSDate * hebrewDate = [hebrew dateFromComponents:components];

NSLog(@"date: %@", date);
NSLog(@"hebrew: %@", hebrewDate);

On my machine, these logs are:

date: 2011-01-09 23:20:39 -0800
hebrew: 1751-09-25 23:20:39 -0800

If you want to convert the material to a more readable format, you use NSDateFormatter:

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setCalendar:gregorian]; //this is usually unnecessary; it here for clarity

NSLog(@"date: %@", [formatter stringFromDate:date]);

[formatter setCalendar:hebrew];

NSLog(@"hebrew: %@", [formatter stringFromDate:hebrewDate]);
[formatter release];

This magazine:

date: January 9, 2011
hebrew: Tishri 9, 2011

It seems to be NSDateFormatterusing a gregorian date, but at least it got the correct month name, doesn't it?

change

-, . NSDateFormatter, , . :

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setCalendar:hebrew];

NSLog(@"hebrew: %@", [formatter stringFromDate:[NSDate date]]);
[formatter release];

:

hebrew: Shevat 4, 5771

! Cocoa ?

+13

Source: https://habr.com/ru/post/1784478/


All Articles