我用今天的日期做标签,但是我需要今天的希伯来语日期。
这是我的代码
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormat stringFromDate:today];
[_label setText:dateString];
我没有用今天的日期来建立这个标签的日历。
谢谢!
最佳答案
假设未为希伯来语设置用户的当前语言环境,那么您需要确保为希伯来语设置日期格式程序的语言环境,
NSLocale *hebrew = [[NSLocale alloc] initWithLocaleIdentifier:@"he_IL"]; // Hebrew, Israel
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = hebrew;
[dateFormat setDateStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormat stringFromDate:today];
[_label setText:dateString];
该代码仍将使用日历作为用户当前的语言环境(例如Gregorian)。如果您还需要希伯来语日历,则需要这样做:
NSLocale *hebrew = [[NSLocale alloc] initWithLocaleIdentifier:@"he_IL"]; // Hebrew, Israel
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = hebrew;
dataFormat.calendar = calendar;
[dateFormat setDateStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormat stringFromDate:today];
[_label setText:dateString];
关于ios - NSDate希伯来语日期标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109624/