Date Formatters
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:.
Note with the Unicode format string format, you should enclose literal text in the format string between apostrophes ('').
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
Custom Formats for User-Visible Dates
使用本地时间
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);
Wed, Jan 25
使用异地时间
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *usFormatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options:0 locale:usLocale];
NSLog(@"usFormatterString: %@", usFormatString);
// Output: usFormatterString: EEE, MMM d Mon, Jan 3
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSString *gbFormatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options:0 locale:gbLocale];
NSLog(@"gbFormatterString: %@", gbFormatString);
// Output: gbFormatterString: EEE d MMM Mon 31 Jan
Formatters and User Interface Elements
1.直接从IB中拖拽Formatters到对象上,然后进行设置
2.用代码添加的方式
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[[textField cell] setFormatter:numberFormatter];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[[contactsForm cells] makeObjectsPerformSelector:@selector(setFormatter:)
withObject:dateFormatter]
- When the cell needs to display or edit its value, it passes its object to the formatter which returns the formatted string. When the user enters a string, or when a string is programmatically written in a cell (usingsetStringValue), the cell obtains the corresponding object from the formatter.