NSCalendar calendarIdentifier
- (NSString *)calendarIdentifier
Return Value of [NSCalendar calendarIdentifier]
The identifier for the receiver. For valid identifiers, see NSLocale.
NSCalendar calendarIdentifier example.
NSCalendar *calendarIdentifier = [[NSCalendar currentCalendar] calendarIdentifier];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:calendarIdentifier] autorelease];
[calendar setFirstWeekday:2];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:calendarIdentifier] autorelease];
[calendar setFirstWeekday:2];
Example of [NSCalendar calendarIdentifier].
- (NSInteger)daysApartFrom:(NSDate *)startDate toDate:(NSDate *)endDate usingCalendar:(NSCalendar *)localizedTZCal {
static NSTimeZone *timeZoneUtc;
static NSCalendar *utcCal;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
timeZoneUtc = [NSTimeZone timeZoneWithName:@"UTC"];
utcCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
utcCal.timeZone = timeZoneUtc;
});
NSInteger componentFlags = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *componentsStartDate = [localizedTZCal components:componentFlags fromDate:startDate];
NSDateComponents *componentsEndDate = [localizedTZCal components:componentFlags fromDate:endDate];
NSDate *utcStartDate = [utcCal dateFromComponents:componentsStartDate];
NSDate *utcEndDate = [utcCal dateFromComponents:componentsEndDate];
NSInteger utcDaysDiff = [self daysWithinEraFromDate:utcStartDate toDate:utcEndDate usingCalendar:utcCal];
return utcDaysDiff;
}
static NSTimeZone *timeZoneUtc;
static NSCalendar *utcCal;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
timeZoneUtc = [NSTimeZone timeZoneWithName:@"UTC"];
utcCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
utcCal.timeZone = timeZoneUtc;
});
NSInteger componentFlags = NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *componentsStartDate = [localizedTZCal components:componentFlags fromDate:startDate];
NSDateComponents *componentsEndDate = [localizedTZCal components:componentFlags fromDate:endDate];
NSDate *utcStartDate = [utcCal dateFromComponents:componentsStartDate];
NSDate *utcEndDate = [utcCal dateFromComponents:componentsEndDate];
NSInteger utcDaysDiff = [self daysWithinEraFromDate:utcStartDate toDate:utcEndDate usingCalendar:utcCal];
return utcDaysDiff;
}
NSCalendar calendarIdentifier example.
[self convertGregorianYearToLocaleYear:NSRepublicOfChinaCalendar withYear:1995];
// 84
[self convertGregorianYearToLocaleYear:NSRepublicOfChinaCalendar withYear:2011];
// 100
NSCalendar *calendar = [NSCalendar currentCalendar];
[self convertGregorianYearToLocaleYear:[calendar calendarIdentifier] withYear:2010];
// 84
[self convertGregorianYearToLocaleYear:NSRepublicOfChinaCalendar withYear:2011];
// 100
NSCalendar *calendar = [NSCalendar currentCalendar];
[self convertGregorianYearToLocaleYear:[calendar calendarIdentifier] withYear:2010];