Date Formatters
There are two basic methods you use to create a string representation of a date and to parse a string to get a date object using a date formatter—dateFromString: and stringFromDate: respectively. You can also use getObjectValue:forString:range:error: if you need more control over the range of a string you want to parse.
There are many attributes you can get and set on a date formatter. When you present information to the user, you should typically simply use the NSDateFormatter style constants to specify pre-defined sets of attributes that determine how a formatted date is displayed. If you need to generate a representation of a date in a precise format, however, you should use a format string.
If you need to parse a date string, the approach you take again depends on what you want to accomplish. If you want to parse input from the user, you should typically use the style constants so as to match their expectations. If you want to parse dates you get from a database or a web service, for example, you should use a format string.
In all cases, you should consider that formatters default to using the user’s locale (currentLocale) superimposed with the user’s preference settings. If you want to use the user’s locale but without their individual settings, you can get the locale id from the current user locale (localeIdentifier) and make a new "standard” locale with that, then set the standard locale as the formatter’s locale.
Use Formatter Styles to Present Dates and Times With the User’s Preferences
NSDateFormatterNoStyle 不显示
NSDateFormatterShortStyle 1/25/12 7:11 PM
NSDateFormatterMediumStyle Jan 25, 2012 7:14:40 PM
NSDateFormatterLongStyle January 25, 2012 7:15:28 PM GMT+08:00
NSDateFormatterFullStyle Wednesday, January 25, 2012 7:16:34 PM China Standard Time
Listing 1 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"
Use Format Strings to Specify Custom Formats 用格式化字符串来指定自定义格式
Fixed Formats
To specify a custom fixed format for a date formatter, you use setDateFormat:. The format string uses the format patterns from the Unicode Technical Standard #35. The version of the standard varies with release of the operating system:
Although in principle a format string specifies a fixed format, by default NSDateFormater still takes the user’s preferences (including the locale setting) into account. You must consider the following points when using format strings:
- NSDateFormatter treats the numbers in a string you parse as if they were in the user’s chosen calendar. For example, if the user selects the Buddhist calendar, parsing the year 2010 yields an NSDate object in 1467 in the Gregorian calendar. (For more about different calendrical systems and how to use them, see Date and Time Programming Guide.)
- In iOS, the user can override the default AM/PM versus 24-hour time setting. This may cause NSDateFormatter to rewrite the format string you set.
Note with the Unicode format string format, you should enclose literal text in the format string between apostrophes ('').
The following example illustrates using a format string to generate a string:
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