Converting Objects

- dateFromString:

Returns a date representation of a given string interpreted using the receiver’s current settings.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@",[dateFormatter dateFromString:@"2014-12-12"]);
		

Output

2014-04-10 06:40:05.986 iOS-Tutorial[988:a0b] 2014-12-12 00:00:00 +0000
		

- stringFromDate:

Returns a string representation of a given date formatted using the receiver’s current settings.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@",[dateFormatter stringFromDate:[NSDate date]]);
		

Output

2014-04-10 06:42:02.977 iOS-Tutorial[1010:a0b] 2014-04-10
		

+ localizedStringFromDate:dateStyle:timeStyle:

Returns string representation of a given date formatted for the current locale using the specified date and time styles.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterFullStyle]);
NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle]);
NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterFullStyle]);
NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle]);
NSLog(@"%@",[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]);
		

Output

2014-04-10 06:47:12.768 iOS-Tutorial[1054:a0b] Apr 10, 2014, 6:47:12 AM India Standard Time
2014-04-10 06:47:12.769 iOS-Tutorial[1054:a0b] Apr 10, 2014
2014-04-10 06:47:12.770 iOS-Tutorial[1054:a0b] 6:47:12 AM India Standard Time
2014-04-10 06:47:12.770 iOS-Tutorial[1054:a0b] Apr 10, 2014, 6:47:12 AM
2014-04-10 06:47:12.771 iOS-Tutorial[1054:a0b] 4/10/14, 6:47 AM
		

Managing Formats and Styles

- dateFormat

Returns the date format string used by the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@",[dateFormatter dateFormat]);
		

Output

2014-04-10 06:53:35.937 iOS-Tutorial[1071:a0b] yyyy-MM-dd
		

- setDateFormat:

Sets the date format for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@",[dateFormatter dateFormat]);
		

Output

2014-04-10 06:53:35.937 iOS-Tutorial[1071:a0b] yyyy-MM-dd
		

- dateStyle

Returns the date style of the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(@"%d",[dateFormatter dateStyle]);
		

Output

2014-04-10 06:56:21.632 iOS-Tutorial[1090:a0b] 1
		

- setDateStyle:

Sets the date style of the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(@"%d",[dateFormatter dateStyle]);
		

Output

2014-04-10 06:56:21.632 iOS-Tutorial[1090:a0b] 1
		

- timeStyle

Returns the time style of the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"%d",[dateFormatter timeStyle]);
		

Output

2014-04-10 06:58:53.112 iOS-Tutorial[1106:a0b] 1
		

- setTimeStyle:

Sets the time style of the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"%d",[dateFormatter timeStyle]);
		

Output

2014-04-10 06:58:53.112 iOS-Tutorial[1106:a0b] 1
		

+ dateFormatFromTemplate:options:locale:

Returns a localized date format string representing the given date format components arranged appropriately for the specified locale.

Example

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *dateComponents = @"yyyy-MM-dd";
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
NSLog(@"%@",dateFormat);
		

Output

2014-04-10 07:05:54.386 iOS-Tutorial[1147:a0b] MM/dd/yyyy
		

Managing Attributes

- calendar

Returns the calendar for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setCalendar:[NSCalendar currentCalendar]];
NSLog(@"%@",[dateFormatter calendar]);
		

Output

2014-04-10 07:09:17.145 iOS-Tutorial[1166:a0b] <_NSCopyOnWriteCalendarWrapper: 0xa152af0>
		

- setCalendar:

Sets the calendar for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setCalendar:[NSCalendar currentCalendar]];
NSLog(@"%@",[dateFormatter calendar]);
		

Output

2014-04-10 07:09:17.145 iOS-Tutorial[1166:a0b] <_NSCopyOnWriteCalendarWrapper: 0xa152af0>
		

- defaultDate

Returns the default date for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDefaultDate:[NSDate date]];
NSLog(@"%@",[dateFormatter defaultDate]);
		

Output

2014-04-10 07:19:30.209 iOS-Tutorial[1199:a0b] 2014-04-10 01:49:30 +0000
		

- setDefaultDate:

Sets the default date for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDefaultDate:[NSDate date]];
NSLog(@"%@",[dateFormatter defaultDate]);
		

Output

2014-04-10 07:19:30.209 iOS-Tutorial[1199:a0b] 2014-04-10 01:49:30 +0000
		

- locale

Returns the locale for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSLog(@"%@",[dateFormatter locale]);
		

Output

2014-04-10 07:22:00.878 iOS-Tutorial[1223:a0b] <__NSCFLocale: 0xa04af90>
		

- setLocale:

Sets the locale for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSLog(@"%@",[dateFormatter locale]);
		

Output

2014-04-10 07:22:00.878 iOS-Tutorial[1223:a0b] <__NSCFLocale: 0xa04af90>
		

- timeZone

Returns the time zone for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@",[dateFormatter timeZone])
		

Output

2014-04-10 07:23:18.571 iOS-Tutorial[1236:a0b] GMT (GMT) offset 0
		

- setTimeZone:

Sets the time zone for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@",[dateFormatter timeZone])
		

Output

2014-04-10 07:23:18.571 iOS-Tutorial[1236:a0b] GMT (GMT) offset 0
		

- twoDigitStartDate

Returns the earliest date that can be denoted by a two-digit year specifier.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@",[dateFormatter twoDigitStartDate]);
		

Output

2014-04-10 07:24:06.558 iOS-Tutorial[1249:a0b] 1950-01-01 00:00:00 +0000
		

- setTwoDigitStartDate:

Sets the two-digit start date for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setTwoDigitStartDate:[NSDate date]];
NSLog(@"%@",[dateFormatter twoDigitStartDate]);
		

Output

2014-04-10 07:25:36.566 iOS-Tutorial[1264:a0b] 2014-04-10 01:55:36 +0000
		

- gregorianStartDate

Returns the start date of the Gregorian calendar for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setGregorianStartDate:[NSDate date]];
NSLog(@"%@",[dateFormatter gregorianStartDate]);
		

Output

2014-04-10 07:26:27.624 iOS-Tutorial[1279:a0b] 2014-04-10 01:56:27 +0000
		

- setGregorianStartDate:

Sets the start date of the Gregorian calendar for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setGregorianStartDate:[NSDate date]];
NSLog(@"%@",[dateFormatter gregorianStartDate]);
		

Output

2014-04-10 07:26:27.624 iOS-Tutorial[1279:a0b] 2014-04-10 01:56:27 +0000
		

Managing AM and PM Symbols

- AMSymbol

Returns the AM symbol for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSLog(@"%@",[dateFormatter AMSymbol]);
		

Output

2014-04-10 07:28:01.961 iOS-Tutorial[1304:a0b] AM
		

- setAMSymbol:

Sets the AM symbol for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd a"];
[dateFormatter setAMSymbol:@"am"];
NSLog(@"%@",[dateFormatter stringFromDate:[NSDate date]]);
		

Output

2014-04-10 07:33:55.587 iOS-Tutorial[1345:a0b] 2014-04-10 am
		

- PMSymbol

Returns the PM symbol for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSLog(@"%@",[dateFormatter PMSymbol]);
		

Output

2014-04-10 07:28:01.961 iOS-Tutorial[1304:a0b] PM
		

- setPMSymbol:

Sets the PM symbol for the receiver.

Example

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd a"];
[dateFormatter setPMSymbol:@"pm"];
NSLog(@"%@",[dateFormatter stringFromDate:[NSDate date]]);
		

Output

2014-04-10 07:33:55.587 iOS-Tutorial[1345:a0b] 2014-04-10 pm
		

Advertisements