Date Formatters
来源:互联网 发布:数据质量检测报告 编辑:程序博客网 时间:2024/06/06 09:47
Date Formatters
有两种基本方法,用来从一个字符串中创建一个 date 对象和从一个 date 对象中解析成一个字符串,dateFromString: and stringFromDate:,如果你需要对解析的字符串进行更多的控制,可以使用 getObjectValue:forString:range:error:方法,
Use Formatter Styles to Present Dates and Times With the User’s Preferences
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".
- For fixed format strings, like Internet dates.
- For user-visible elements that don’t match any of the existing styles
可以通过使用setDateFormat:方法来设置自定义的格式,这些格式需要遵循 the Unicode Technical Standard #35,
使用自定义字符串格式时,还需要考虑两件事情
- 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.
下面是如何使用自定义字符串格式的示例:
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
There are two things to note about this example:
- It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
- The representation of the time may be 13:00. In iOS, however, if the user has switched 24-Hour Time to Off, the time may be 1:00 pm.
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);
什么情况下你会需要使用这种方式呢?假设你现在需要显示某个时间的名称,日,和月,你是没办法使用自定义的格式来实现这个功能的,
- 因为没有能够忽略年的自定义格式,
- 因为无法统一的对不同地区使用相同的格式,例如,上述代码,在美国显示结果是EEE, MMM d,在德国是EEE d MMM.
- (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. */ NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init]; NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; [rfc3339DateFormatter setLocale:enUSPOSIXLocale]; [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"]; [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; // Convert the RFC 3339 date time string to an NSDate. NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString]; NSString *userVisibleDateTimeString; if (date != nil) { // Convert the date object to a user-visible date string. NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init]; assert(userVisibleDateFormatter != nil); [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle]; [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle]; userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date]; } return userVisibleDateTimeString;}
创建一个时间格式开销很大,如果需要频繁使用格式化,更有效的办法是创建一个单例,
下面是一个示例代码
static NSDateFormatter *sUserVisibleDateFormatter = nil;- (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. */ // If the date formatters aren't already set up, create them and cache them for reuse. static NSDateFormatter *sRFC3339DateFormatter = nil; if (sRFC3339DateFormatter == nil) { sRFC3339DateFormatter = [[NSDateFormatter alloc] init]; NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; [sRFC3339DateFormatter setLocale:enUSPOSIXLocale]; [sRFC3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"]; [sRFC3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; } // Convert the RFC 3339 date time string to an NSDate. NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString]; NSString *userVisibleDateTimeString; if (date != nil) { if (sUserVisibleDateFormatter == nil) { sUserVisibleDateFormatter = [[NSDateFormatter alloc] init]; [sUserVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle]; [sUserVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle]; } // Convert the date object to a user-visible date string. userVisibleDateTimeString = [sUserVisibleDateFormatter stringFromDate:date]; } return userVisibleDateTimeString;}
你的时间格式应该响应用户地区变化,通过监听NSCurrentLocaleDidChangeNotification可以实现这点,
Be aware that the C library also has the idea of a current locale. To guarantee a fixed date format, you should pass NULL as the loc parameter of these routines. This causes them to use the POSIX locale (also known as the C locale), which is equivalent to Cocoa's en_US_POSIX locale
示例代码:
struct tm sometime;const char *formatString = "%Y-%m-%d %H:%M:%S %z";(void) strptime_l("2005-07-01 12:00:00 -0700", formatString, &sometime, NULL);NSLog(@"NSDate is %@", [NSDate dateWithTimeIntervalSince1970: mktime(&sometime)]);// Output: NSDate is 2005-07-01 12:00:00 -0700
- Date Formatters
- 自定义 Formatters
- Coding templates and formatters
- Number Formatters 格式…
- The String Formatters of Manor Farm(zz)
- ConfigParser.NoSectionError: No section: 'formatters'原因及解决办法
- ConfigParser.NoSectionError: No section: 'formatters'原因及解决办法
- AngularJs之ngModel中$parsers和$formatters随笔
- angualr 自定义指令link函数NgModelController Formatters 和 parsers
- AngularJS初涉之$parsers与$formatters、$apply与$digest
- date
- date
- date
- Date
- date
- date
- date
- date
- centos上squid普通代理服务器设置
- oracle数据库SQL语句
- 【Server】Socket编程原理详解
- eclipse/myeclipse一些常用的快捷键
- Linux内核模块
- Date Formatters
- iOS Developer Library
- Qt 之QRegExp
- 22.PHP内核探索:PHP哈希算法设计
- [Hbase]eclipse下操作hbase
- PHP中$GLOBALS和global的区别
- Android基础积累
- 1034. Forest
- 修改system.img的文件的权限和属性:使用make.ext4fs的方法