System Locale Information

+ currentCalendar

Returns the logical calendar for the current user.

Example

NSCalendar *calender = [NSCalendar currentCalendar];
NSLog(@"%@",calender);
		

Output

2014-04-08 06:28:32.589 iOS-Tutorial[15532:a0b] <_NSCopyOnWriteCalendarWrapper: 0x8d54050>
		

+ autoupdatingCurrentCalendar

Returns the current logical calendar for the current user.

Example

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSLog(@"%@",calender);	
	

Output

2014-04-08 06:30:48.933 iOS-Tutorial[15816:a0b] Auto-updating Calendar  [<_NSCopyOnWriteCalendarWrapper: 0xa2c3780>]
		

Initializing a Calendar

- initWithCalendarIdentifier:

Initializes a newly-allocated NSCalendar object for the calendar specified by a given identifier.

Example

NSCalendar *calender = [[NSCalendar alloc]initWithCalendarIdentifier:@"gregorian"];
NSLog(@"%@",calender);
		

Output

2014-04-08 06:39:07.874 iOS-Tutorial[16355:a0b] <_NSCopyOnWriteCalendarWrapper: 0x8d17150>
		

- setFirstWeekday:

Sets the index of the first weekday for the receiver.

Example

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
[calender setFirstWeekday:1];
NSLog(@"%d",[calender firstWeekday]);
		

Output

2014-04-08 06:41:11.626 iOS-Tutorial[16471:a0b] 1
		

- setLocale:

Sets the locale for the receiver.

Example

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
[calender setLocale:[NSLocale currentLocale]];
NSLog(@"%@",[calender locale]);
		

Output

2014-04-08 06:43:28.723 iOS-Tutorial[16525:a0b] <__NSCFLocale: 0x8d72af0>
		

- setMinimumDaysInFirstWeek:

Sets the minimum number of days in the first week of the receiver.

Example

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
[calender setMinimumDaysInFirstWeek:7];
NSLog(@"%d",[calender minimumDaysInFirstWeek]);		
	

Output

2014-04-08 06:44:55.157 iOS-Tutorial[16537:a0b] 7
		

- setTimeZone:

Sets the time zone for the receiver.

Example

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
[calender setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(@"%@",[calender timeZone]);
		

Output

2014-04-08 06:46:51.208 iOS-Tutorial[16549:a0b] GMT (GMT) offset 0
		

Calendrical Calculations

- components:fromDate:

Returns a NSDateComponents object containing a given date decomposed into specified components.

Example

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
[calender setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(@"%@",[calender components:NSCalendarUnitDay fromDate:[NSDate date]]);
NSLog(@"%@",[calender components:NSCalendarUnitYear fromDate:[NSDate date]]);
NSLog(@"%@",[calender components:NSCalendarUnitMonth fromDate:[NSDate date]]);
		

Output

2014-04-08 06:49:07.693 iOS-Tutorial[16585:a0b] 
    Day: 8
2014-04-08 06:49:07.694 iOS-Tutorial[16585:a0b] 
    Calendar Year: 2014
2014-04-08 06:49:07.694 iOS-Tutorial[16585:a0b] 
    Month: 4
    Leap month: no
		

- components:fromDate:toDate:options:

Returns, as an NSDateComponents object using specified components, the difference between two supplied dates.

Example

 NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
    [calender setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(@"%@",[calender components:NSCalendarUnitYear fromDate:[NSDate dateWithTimeIntervalSince1970:0] toDate:[NSDate dateWithTimeIntervalSinceNow:18000] options:NSCalendarWrapComponents]);
		

Output

2014-04-08 06:51:10.761 iOS-Tutorial[16610:a0b] 
    Calendar Year: 44
		

- dateByAddingComponents:toDate:options:

Returns a new NSDate object representing the absolute time calculated by adding given components to a given date.

Example

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComponent = [[NSDateComponents alloc]init];
[dateComponent setYear:10];
NSLog(@"%@",[calender dateByAddingComponents:dateComponent toDate:[NSDate dateWithTimeIntervalSinceNow:0] options:NSCalendarWrapComponents] );
		

Output

2014-04-08 06:57:29.105 iOS-Tutorial[16650:a0b] 2024-04-08 01:27:29 +0000
		

- dateFromComponents:

Returns a new NSDate object representing the absolute time calculated from given components.

Example

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComponent = [[NSDateComponents alloc]init];
[dateComponent setYear:2020];
NSLog(@"%@",[calender dateFromComponents:dateComponent]);
		

Output

2014-04-08 06:58:59.230 iOS-Tutorial[16676:a0b] 2019-12-31 18:30:00 +0000
		

Advertisements