{"id":308,"date":"2010-05-26T12:57:04","date_gmt":"2010-05-26T04:57:04","guid":{"rendered":"http:\/\/www.xujiwei.com\/blog\/?p=308"},"modified":"2011-05-29T12:27:51","modified_gmt":"2011-05-29T04:27:51","slug":"format-date-and-parse-date","status":"publish","type":"post","link":"https:\/\/xujiwei.com\/blog\/2010\/05\/format-date-and-parse-date\/","title":{"rendered":"[Cocoa\u5b66\u4e60\uff3d\u683c\u5f0f\u5316\u65e5\u671f\u548c\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u663e\u793a\u6d88\u606f\u7684\u65f6\u5019\u4e00\u822c\u90fd\u9700\u8981\u9644\u5e26\u4e00\u4e2a\u6d88\u606f\u63a5\u6536\u7684\u65f6\u95f4\uff0c\u4f7f\u7528 NSDate \u548c NSDateFormatter \u5373\u53ef\u5b8c\u6210\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f\u7684\u4efb\u52a1\uff0c\u4f46\u662f\u5728\u4f7f\u7528\u65f6\u4f1a\u6bd4\u8f83\u9ebb\u70e6\uff0c\u901a\u8fc7 Category \u7684\u65b9\u5f0f\u6765\u7ed9 NSDate \u7c7b\u76f4\u63a5\u6dfb\u52a0\u4e24\u4e2a\u7c7b\u65b9\u6cd5\u6765\u505a\u8fd9\u4e9b\u4e8b\u60c5\u4f1a\u6bd4\u8f83\u597d\uff0c\u5728\u4ee5\u540e\u9700\u8981\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u65f6\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u53bb\u521b\u5efa\u4e00\u4e2a NSDateFormatter \u5bf9\u8c61\u3002<\/p>\n<p>\u5bf9\u4e8e\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u4e5f\u662f\u540c\u7406\u3002<\/p>\n<pre lang=\"objc\">\r\n@interface NSDate (FormatString)\r\n\r\n- (NSString*)stringWithFormat:(NSString*)fmt;\r\n+ (NSDate*)dateFromString:(NSString*)str withFormat:(NSString*)fmt;\r\n\r\n@end\r\n\r\n@implementation NSDate (FormatString)\r\n\r\n- (NSString*)stringWithFormat:(NSString*)fmt {\r\n    static NSDateFormatter *fmtter;\r\n    \r\n    if (fmtter == nil) {\r\n        fmtter = [[NSDateFormatter alloc] init];\r\n    }\r\n    \r\n    if (fmt == nil || [fmt isEqualToString:@\"\"]) {\r\n        fmt = @\"HH:mm:ss\";\r\n    }\r\n    \r\n    [fmtter setDateFormat:fmt];\r\n    \r\n    return [fmtter stringFromDate:self];\r\n}\r\n\r\n+ (NSDate*)dateFromString:(NSString*)str withFormat:(NSString*)fmt {\r\n    static NSDateFormatter *fmtter;\r\n    \r\n    if (fmtter == nil) {\r\n        fmtter = [[NSDateFormatter alloc] init];\r\n    }\r\n    \r\n    if (fmt == nil || [fmt isEqualToString:@\"\"]) {\r\n        fmt = @\"HH:mm:ss\";\r\n    }\r\n    \r\n    [fmtter setDateFormat:fmt];\r\n    \r\n    return [fmtter dateFromString:str];\r\n}\r\n\r\n@end\r\n<\/pre>\n<p>\u5728\u8fd9\u91cc\uff0cdataFromString \u548c stringWithFormat \u5982\u679c\u6ca1\u6709\u6307\u5b9a\u683c\u5f0f\u5b57\u7b26\u4e32\u53c2\u6570\u7684\u8bdd\uff0c\u9ed8\u8ba4\u4f1a\u4f7f\u7528 HH:mm:ss \u6765\u683c\u5f0f\u65e5\u671f\uff0c\u5f53\u7136\uff0c\u5982\u679c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u4e0d\u4e00\u6837\uff0c\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u4fee\u6539\u3002<\/p>\n<p>\u5982\u679c\u9700\u8981\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4e5f\u53ef\u4ee5\u53bb\u4fee\u6539\u4e24\u4e2a\u65b9\u6cd5\u6765\u7b26\u5408\u81ea\u5df1\u7684\u9700\u8981\u3002<\/p>\n<pre lang=\"objc\">\r\n\/\/ \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\r\nNSString *now = [[NSDate date] stringWithFormat:@\"HH:mm:ss\"];\r\n\r\n\/\/ \u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u89e3\u6790\u6210 NSDate \u5bf9\u8c61\r\nNSStirng *recvTime = @\"2010-05-25 16:58:34 +0800\";\r\nNSDate  *recv = [NSDate dateFromString:recvTime withFormat:@\"yyyy-MM-dd HH:mm:ss Z\"];\r\n<\/pre>\n<p>\u4f7f\u7528 Category \u7ed9 NSDate \u7c7b\u6dfb\u52a0\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u540e\uff0c\u518d\u8981\u683c\u5f0f\u5316\u65e5\u671f\u6216\u8005\u89e3\u6790\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5c31\u4e0d\u9700\u8981\u989d\u5916\u53bb\u521b\u5efa NSDateFormatter \u5bf9\u8c61\u4e86\uff0c\u53ef\u4ee5\u7701\u4e0d\u5c11\u529b\u6c14\u3002 <\/p>\n<p><strong>\u53c2\u8003\u8d44\u6599<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/399527\/parsing-unsupported-date-formats-in-via-cocoas-nsdate\">Parsing unsupported date formats in via Cocoa\u2019s NSDate<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/822417\/why-cant-i-correctly-parse-this-date-string-with-nsdateformatter\">Why can\u2019t I correctly parse this date string with NSDateFormatter?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u663e\u793a\u6d88\u606f\u7684\u65f6\u5019\u4e00\u822c\u90fd\u9700\u8981\u9644\u5e26\u4e00\u4e2a\u6d88\u606f\u63a5\u6536\u7684\u65f6\u95f4\uff0c\u4f7f\u7528 NSDate &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/xujiwei.com\/blog\/2010\/05\/format-date-and-parse-date\/\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168],"tags":[144,145,146],"_links":{"self":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/308"}],"collection":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/comments?post=308"}],"version-history":[{"count":0,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"wp:attachment":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}