[NSDateFormatter setDateFormat] is used to specify how to generate NSString object from the given NSDate object. [
NSDateFormatter setDateFormat] takes a format specifier which has its own rules. You can find out more information on iPhone developer site.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
[
NSDateFormatter setDateFormat example]
Declaration.
- (void)
setDateFormat:(
NSString *)
string
Parameters
- string
- The date format for the receiver. See Data Formatting Guide for a list of the conversion specifiers permitted in date format strings.
------------------------------------------------------------------------
[Example 1]
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy :EEEE"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"date: %@", dateString);
[dateFormat release];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
[components setDay:([components day]-([components weekday]-1))];
NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:@"MM/dd/yyyy :EEEE"];
NSString *dateString_first = [dateFormat_first stringFromDate:beginningOfWeek];
NSLog(@"First_date: %@", dateString_first);
--------------------------------------------------------------------
[Example 2 from Apple Developer Site]
// Formatting a date using formatter styles
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
|
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
|
|
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
|
|
NSString *formattedDateString = [dateFormatter stringFromDate:date];
|
NSLog(@"formattedDateString: %@", formattedDateString);
|
// Output for locale en_US: "formattedDateString: Jan 2, 2001"
|
---------------------------------------------------------------------
[Example 3 from Apple Developer Site]
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
|
|
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
|
|
NSString *formattedDateString = [dateFormatter stringFromDate:date];
|
NSLog(@"formattedDateString: %@", formattedDateString);
|
// For US English, the output may be:
|
// formattedDateString: 2001-01-02 at 13:00
|
---------------------------------------------------------------------
[Example 4 from Apple Developer Site]
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options:0
|
locale:[NSLocale currentLocale]];
|
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
[dateFormatter setDateFormat:formatString];
|
|
NSString *todayString = [dateFormatter stringFromDate:[NSDate date]];
|
NSLog(@"todayString: %@", todayString);
|
---------------------------------------------------------------------
[Example 5 from Apple Developer Site]
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
|
NSString *usFormatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options:0 locale:usLocale];
|
NSLog(@"usFormatterString: %@", usFormatString);
|
// Output: usFormatterString: EEE, MMM d
|
|
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
|
NSString *gbFormatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options:0 locale:gbLocale];
|
NSLog(@"gbFormatterString: %@", gbFormatString);
|
// Output: gbFormatterString: EEE d MMM
|
---------------------------------------------------------------------
[Example 6 from Apple Developer Site]
- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
|
// Returns a user-visible date time string that corresponds to the
|
// specified RFC 3339 date time string. Note that this does not handle
|
// all possible RFC 3339 date time strings, just one of the most common
|
// styles.
|
{
|
NSString * userVisibleDateTimeString;
|
NSDateFormatter * rfc3339DateFormatter;
|
NSLocale * enUSPOSIXLocale;
|
NSDate * date;
|
NSDateFormatter * userVisibleDateFormatter;
|
|
userVisibleDateTimeString = nil;
|
|
// Convert the RFC 3339 date time string to an NSDate.
|
|
rfc3339DateFormatter = [[[NSDateFormatter alloc] init] autorelease];
|
|
enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
|
|
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
|
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
|
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
|
|
date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
|
if (date != nil) {
|
|
// Convert the NSDate to a user-visible date string.
|
|
userVisibleDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
|
assert(userVisibleDateFormatter != nil);
|
|
[userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
|
[userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];
|
|
userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
|
}
|
return userVisibleDateTimeString;
|
}
|