Advertisements
NSDateComponents by example
Getting Information About an NSDateComponents Object
- era
Returns the number of era units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setEra:1]; NSLog(@"%d", [dateComponents era]);
Output
2014-04-10 05:15:10.497 iOS-Tutorial[519:a0b] 1
- year
Returns the number of year units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; NSLog(@"%d", [dateComponents year]);
Output
2014-04-10 05:17:36.520 iOS-Tutorial[534:a0b] 2014
- month
Returns the number of month units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setMonth:12]; NSLog(@"%d", [dateComponents month]);
Output
2014-04-10 05:19:04.089 iOS-Tutorial[566:a0b] 12
- date
Returns the date of the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%@", [dateComponents date]);
Output
2014-04-10 05:41:11.371 iOS-Tutorial[711:a0b] 2014-04-12 06:42:12 +0000
- day
Returns the number of day units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%d", [dateComponents day]);
Output
2014-04-10 05:41:46.623 iOS-Tutorial[723:a0b] 12
- hour
Returns the number of hour units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%d", [dateComponents hour]);
Output
2014-04-10 05:41:46.623 iOS-Tutorial[723:a0b] 12
- minute
Returns the number of minute units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%d", [dateComponents minute]);
Output
2014-04-10 05:41:46.623 iOS-Tutorial[723:a0b] 12
- second
Returns the number of second units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%d", [dateComponents second]);
Output
2014-04-10 05:41:46.623 iOS-Tutorial[723:a0b] 12
- week
Returns the number of week units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; [dateComponents setWeek:5]; NSLog(@"%d", [dateComponents week]);
Output
2014-04-10 05:41:46.623 iOS-Tutorial[723:a0b] 5
- weekday
Returns the number of weekday units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; [dateComponents setWeekday:5]; NSLog(@"%d", [dateComponents weekday]);
Output
2014-04-10 05:41:46.623 iOS-Tutorial[723:a0b] 5
- weekdayOrdinal
Returns the ordinal number of weekday units for the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; [dateComponents setWeekdayOrdinal:5]; NSLog(@"%d", [dateComponents weekdayOrdinal]);
Output
2014-04-10 05:46:31.462 iOS-Tutorial[738:a0b] 5
- quarter
Returns the number of quarters in the calendar.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; [dateComponents setQuarter:3]; NSLog(@"%d", [dateComponents quarter]);
Output
2014-04-10 05:47:24.593 iOS-Tutorial[753:a0b] 3
- calendar
Returns the calendar of the receiver.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%@", [dateComponents calendar]);
Output
2014-04-10 05:48:09.573 iOS-Tutorial[765:a0b] <_NSCopyOnWriteCalendarWrapper: 0xa0591a0>
- timeZone
Returns the receivers time zone.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%@", [dateComponents timeZone]);
Output
2014-04-10 05:48:43.540 iOS-Tutorial[777:a0b] Asia/Kolkata (GMT+5:30) offset 19800
- weekOfMonth
Returns the week of the month.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; [dateComponents setWeekOfMonth:4]; NSLog(@"%d", [dateComponents weekOfMonth]);
Output
2014-04-10 05:50:03.423 iOS-Tutorial[789:a0b] 4
- weekOfYear
Returns the week of the year.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; [dateComponents setWeekOfYear:4]; NSLog(@"%d", [dateComponents weekOfYear]);
Output
2014-04-10 05:53:49.494 iOS-Tutorial[807:a0b] 4
- yearForWeekOfYear
Returns the year for the week of the year.
Example
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; [dateComponents setYearForWeekOfYear:4]; [dateComponents setYear:2014]; [dateComponents setMonth:4]; [dateComponents setDay:12]; [dateComponents setHour:12]; [dateComponents setMinute:12]; [dateComponents setSecond:12]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%d", [dateComponents yearForWeekOfYear]);
Output
2014-04-10 05:55:08.729 iOS-Tutorial[823:a0b] 4
Setting Information for an NSDateComponents Object
- setEra:
Sets the number of era units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setEra:0]; NSLog(@"%d", [dateComponents era]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 0
- setYear:
Sets the number of year units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setYear:2014]; NSLog(@"%d", [dateComponents year]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 2014
- setMonth:
Sets the number of month units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setMonth:4]; NSLog(@"%d", [dateComponents month]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 4
- setDay:
Sets the number of day units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setDay:12]; NSLog(@"%d", [dateComponents day]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 12
- setHour:
Sets the number of hour units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setHour:12]; NSLog(@"%d", [dateComponents hour]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 12
- setMinute:
Sets the number of minute units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setMinute:12]; NSLog(@"%d", [dateComponents minute]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 12
- setSecond:
Sets the number of second units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setSecond:12]; NSLog(@"%d", [dateComponents second]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 12
- setWeek:
Sets the number of week units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setWeek:3]; NSLog(@"%d", [dateComponents week]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 3
- setWeekday:
Sets the number of weekday units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setWeekday:3]; NSLog(@"%d", [dateComponents weekday]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 3
- setWeekdayOrdinal:
Sets the ordinal number of weekday units for the receiver.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setWeekdayOrdinal:3]; NSLog(@"%d", [dateComponents weekdayOrdinal]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 3
- setQuarter:
Sets the number of quarters in the calendar.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setQuarter:3]; NSLog(@"%d", [dateComponents quarter]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 3
- setCalendar:
Sets the receivers calendar.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%@", [dateComponents calendar]);
Output
2014-04-10 06:21:13.565 iOS-Tutorial[900:a0b] <_NSCopyOnWriteCalendarWrapper: 0x893b160>
- setTimeZone:
Sets the receivers time zone.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setCalendar:[NSCalendar currentCalendar]]; [dateComponents setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%@", [dateComponents timeZone]);
Output
2014-04-10 06:20:04.303 iOS-Tutorial[890:a0b] Asia/Kolkata (GMT+5:30) offset 19800
- setWeekOfMonth:
Sets the week of the month.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]];
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 0
- setWeekOfYear:
Sets the week of the year.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setWeekOfYear:3]; NSLog(@"%d", [dateComponents weekOfYear]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 3
- setYearForWeekOfYear:
Sets the year for the week of the year.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setYearForWeekOfYear:3]; NSLog(@"%d", [dateComponents yearForWeekOfYear]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 3
- setLeapMonth:
Sets the month as a leap month.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setLeapMonth:YES]; NSLog(@"%d", [dateComponents isLeapMonth]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 1
- isLeapMonth
Returns whether the month is a leap month.
Example
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit) fromDate:[NSDate date]]; [dateComponents setLeapMonth:YES]; NSLog(@"%d", [dateComponents isLeapMonth]);
Output
2014-04-10 06:06:02.098 iOS-Tutorial[858:a0b] 1
Advertisements