YYKit源碼探究(五十五) —— NSDate分類(lèi)之Date Format(三)

版本記錄

版本號(hào) 時(shí)間
V1.0 2018.03.30

前言

iOS圈內(nèi)有幾個(gè)人大家基本都知道,比如說(shuō)王巍、唐巧,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等。這里有一篇唐巧對(duì)他的專(zhuān)訪,還有他的 GitHub - Yaoyuan博客,這里貼出來(lái)框架YYKit 框架。接下來(lái)幾篇我們就一起來(lái)看一下這個(gè)框架。感興趣的可以看上面寫(xiě)的幾篇。
1. YYKit源碼探究(一) —— 基本概覽
2. YYKit源碼探究(二) —— NSString分類(lèi)之Hash(一)
3. YYKit源碼探究(三) —— NSString分類(lèi)之Encode and decode(二)
4. YYKit源碼探究(四) —— NSString分類(lèi)之Drawing(三)
5. YYKit源碼探究(五) —— NSString分類(lèi)之Regular Expression(四)
6. YYKit源碼探究(六) —— NSString分類(lèi)之NSNumber Compatible(五)
7. YYKit源碼探究(七) —— NSString分類(lèi)之Utilities(六)
8. YYKit源碼探究(八) —— NSNumber分類(lèi)(一)
9. YYKit源碼探究(九) —— UIFont分類(lèi)之架構(gòu)分析和Font Traits(一)
10. YYKit源碼探究(十) —— UIFont分類(lèi)之Create font(二)
11. YYKit源碼探究(十一) —— UIFont分類(lèi)之Load and unload font(三)
12. YYKit源碼探究(十二) —— UIFont分類(lèi)之Dump font data(四)
13. YYKit源碼探究(十三) —— UIImage分類(lèi)之框架結(jié)構(gòu)和Create image部分(一)
14. YYKit源碼探究(十四) —— UIImage分類(lèi)之Image Info(二)
15. YYKit源碼探究(十五) —— UIImage分類(lèi)之Modify Image(三)
16. YYKit源碼探究(十六) —— UIImage分類(lèi)之Image Effect(四)
17. YYKit源碼探究(十七) —— UIImageView分類(lèi)之架構(gòu)和image部分(一)
18. YYKit源碼探究(十八) —— UIImageView分類(lèi)之highlight image部分(二)
19. YYKit源碼探究(十九) —— UIScreen分類(lèi)(一)
20. YYKit源碼探究(二十) —— UIScrollView分類(lèi)(一)
21. YYKit源碼探究(二十一) —— UITableView分類(lèi)(一)
22. YYKit源碼探究(二十二) —— UITextField分類(lèi)(一)
23. YYKit源碼探究(二十三) —— UIView分類(lèi)(一)
24. YYKit源碼探究(二十四) —— UIPasteboard分類(lèi)(一)
25. YYKit源碼探究(二十五) —— UIGestureRecognizer分類(lèi)(一)
26. YYKit源碼探究(二十六) —— UIDevice分類(lèi)框架及Device Information(一)
27. YYKit源碼探究(二十七) —— UIDevice分類(lèi)之Network Information(二)
28. YYKit源碼探究(二十八) —— UIDevice分類(lèi)之Disk Space(三)
29. YYKit源碼探究(二十九) —— UIDevice分類(lèi)之Memory Information(四)
30. YYKit源碼探究(三十) —— UIDevice分類(lèi)之CPU Information(五)
31. YYKit源碼探究(三十一) —— UIControl分類(lèi)(一)
32. YYKit源碼探究(三十二) —— UIColor分類(lèi)之Create a UIColor Object(一)
33. YYKit源碼探究(三十三) —— UIColor分類(lèi)之Get color's description(二)
34. YYKit源碼探究(三十四) —— UIColor分類(lèi)之Retrieving Color Information(三)
35. YYKit源碼探究(三十五) —— UIButton分類(lèi)之image(一)
36. YYKit源碼探究(三十六) —— UIButton分類(lèi)之background image(二)
37. YYKit源碼探究(三十七) —— UIBezierPath分類(lèi)(一)
38. YYKit源碼探究(三十八) —— UIBarButtonItem分類(lèi)(一)
39. YYKit源碼探究(三十九) —— UIApplication分類(lèi)(一)
40. YYKit源碼探究(四十) —— NSTimer分類(lèi)(一)
41. YYKit源碼探究(四十一) —— NSParagraphStyle分類(lèi)(一)
42. YYKit源碼探究(四十二) —— NSObject分類(lèi)之YYModel(一)
43. YYKit源碼探究(四十三) —— NSObject分類(lèi)之KVO(二)
44. YYKit源碼探究(四十四) —— NSObject分類(lèi)之Sending messages with variable parameters(三)
45. YYKit源碼探究(四十五) —— NSObject分類(lèi)之Swap method (Swizzling)(四)
46. YYKit源碼探究(四十六) —— NSObject分類(lèi)之Associate value(五)
47. YYKit源碼探究(四十七) —— NSObject分類(lèi)之Other(六)
48. YYKit源碼探究(四十八) —— NSNotificationCenter分類(lèi)(一)
49. YYKit源碼探究(四十九) —— NSKeyedUnarchiver分類(lèi)(一)
50. YYKit源碼探究(五十) —— NSDictionary分類(lèi)之Dictionary Convertor(一)
51. YYKit源碼探究(五十一) —— NSDictionary分類(lèi)之Dictionary Value Getter(二)
52. YYKit源碼探究(五十二) —— NSDictionary分類(lèi)之NSMutableDictionary(三)
53. YYKit源碼探究(五十三) —— NSDate分類(lèi)之Component Properties(一)
54. YYKit源碼探究(五十四) —— NSDate分類(lèi)之Date modify(二)

回顧

上一篇主要介紹了Date modify分類(lèi)部分,這一篇主要看一下NSDate分類(lèi)的Date Format部分。


API

下面我們看一下API

/**
 Returns a formatted string representing this date.
 see http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
 for format description.
 
 @param format   String representing the desired date format.
 e.g. @"yyyy-MM-dd HH:mm:ss"
 
 @return NSString representing the formatted date string.
 */
- (nullable NSString *)stringWithFormat:(NSString *)format;

/**
 Returns a formatted string representing this date.
 see http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
 for format description.
 
 @param format    String representing the desired date format.
 e.g. @"yyyy-MM-dd HH:mm:ss"
 
 @param timeZone  Desired time zone.
 
 @param locale    Desired locale.
 
 @return NSString representing the formatted date string.
 */
- (nullable NSString *)stringWithFormat:(NSString *)format
                               timeZone:(nullable NSTimeZone *)timeZone
                                 locale:(nullable NSLocale *)locale;

/**
 Returns a string representing this date in ISO8601 format.
 e.g. "2010-07-09T16:13:30+12:00"
 
 @return NSString representing the formatted date string in ISO8601.
 */
- (nullable NSString *)stringWithISOFormat;

/**
 Returns a date parsed from given string interpreted using the format.
 
 @param dateString The string to parse.
 @param format     The string's date format.
 
 @return A date representation of string interpreted using the format.
 If can not parse the string, returns nil.
 */
+ (nullable NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format;

/**
 Returns a date parsed from given string interpreted using the format.
 
 @param dateString The string to parse.
 @param format     The string's date format.
 @param timeZone   The time zone, can be nil.
 @param locale     The locale, can be nil.
 
 @return A date representation of string interpreted using the format.
 If can not parse the string, returns nil.
 */
+ (nullable NSDate *)dateWithString:(NSString *)dateString
                             format:(NSString *)format
                           timeZone:(nullable NSTimeZone *)timeZone
                             locale:(nullable NSLocale *)locale;

/**
 Returns a date parsed from given string interpreted using the ISO8601 format.
 
 @param dateString The date string in ISO8601 format. e.g. "2010-07-09T16:13:30+12:00"
 
 @return A date representation of string interpreted using the format.
 If can not parse the string, returns nil.
 */
+ (nullable NSDate *)dateWithISOFormatString:(NSString *)dateString;

下面我們看一下該API的詳細(xì)信息。

1. - (nullable NSString *)stringWithFormat:(NSString *)format;

該方法的作用就是返回一個(gè)代表日期的格式化字符串。

  • format:代表日期格式的字符串,如@"yyyy-MM-dd HH:mm:ss"

方法實(shí)現(xiàn)

- (NSString *)stringWithFormat:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    [formatter setLocale:[NSLocale currentLocale]];
    return [formatter stringFromDate:self];
}

2. - (nullable NSString *)stringWithFormat:(NSString *)format timeZone:(nullable NSTimeZone *)timeZone locale:(nullable NSLocale *)locale;

該方法的作用就是返回一個(gè)代表日期的格式化字符串。

方法實(shí)現(xiàn)

- (NSString *)stringWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    if (timeZone) [formatter setTimeZone:timeZone];
    if (locale) [formatter setLocale:locale];
    return [formatter stringFromDate:self];
}

3. - (nullable NSString *)stringWithISOFormat;

該方法的作用就是返回一個(gè)代表日期的ISO8601格式的字符串。例如:"2010-07-09T16:13:30+12:00"

方法實(shí)現(xiàn)

- (NSString *)stringWithISOFormat {
    static NSDateFormatter *formatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [[NSDateFormatter alloc] init];
        formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
        formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
    });
    return [formatter stringFromDate:self];
}

4. + (nullable NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format;

該方法的作用就是返回從使用格式解釋的給定字符串解析的日期。

方法實(shí)現(xiàn)

+ (NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    return [formatter dateFromString:dateString];
}

5. + (nullable NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format timeZone:(nullable NSTimeZone *)timeZone locale:(nullable NSLocale *)locale;

該方法的作用就是返回從使用格式解釋的給定字符串解析的日期。

方法實(shí)現(xiàn)

+ (NSDate *)dateWithString:(NSString *)dateString format:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    if (timeZone) [formatter setTimeZone:timeZone];
    if (locale) [formatter setLocale:locale];
    return [formatter dateFromString:dateString];
}

6. + (nullable NSDate *)dateWithISOFormatString:(NSString *)dateString;

該方法的作用就是返回從使用ISO8601格式解釋的給定字符串解析的日期。

方法實(shí)現(xiàn)

+ (NSDate *)dateWithISOFormatString:(NSString *)dateString {
    static NSDateFormatter *formatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [[NSDateFormatter alloc] init];
        formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
        formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
    });
    return [formatter dateFromString:dateString];
}

后記

本篇主要介紹了NSDate分類(lèi)Date Format,感興趣的給個(gè)關(guān)注和贊,謝謝~~~~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容