【精】iOS 日期操作總結:NSDate、NSDateFormatter
阿新 • • 發佈:2019-02-15
1、日期(NSDate)
///////////////////////////////////////////////////////////////////////// // //(1)NSDate 初始化 // ///////////////////////////////////////////////////////////////////////// // 初始化一個當前時刻物件 NSDate *now = [[NSDate alloc] init]; // 初始化一個明天當前時刻物件 NSDate *tomorrow = [[NSDate alloc] initWithTimeIntervalSinceNow:24*60*60]; // 初始化一個昨天當前時刻物件 NSDate *yesterday = [[NSDate alloc] initWithTimeInterval:-24*60*60 sinceDate:now]; // 初始化一個 2001-01-01 08:00:00 1小時後的時刻物件 NSDate *oneHourAfterReferenceDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:60*60]; // 初始化一個 1970-01-01 08:00:00 1小時後的時刻物件 NSDate *oneHourAfter1970 = [[NSDate alloc] initWithTimeIntervalSince1970:60*60]; ///////////////////////////////////////////////////////////////////////// // //(2)獲取日期描述 // ///////////////////////////////////////////////////////////////////////// NSString *dateDescription = now.description; ///////////////////////////////////////////////////////////////////////// // //(3)獲取時間間隔 // ///////////////////////////////////////////////////////////////////////// // 獲取今天到明天的時間間隔 NSTimeInterval interval1 = [tomorrow timeIntervalSinceDate:now]; // 獲取今天到明天的時間間隔 NSTimeInterval interval2 = [tomorrow timeIntervalSinceNow]; // 獲取 2001-01-01 08:00:00 到今天的時間間隔 NSTimeInterval interval3 = [now timeIntervalSinceReferenceDate]; // 獲取 1970-01-01 08:00:00 到今天的時間間隔 NSTimeInterval interval4 = [now timeIntervalSince1970]; ///////////////////////////////////////////////////////////////////////// // //(4)隨機返回一個不可能達到的未來時間、過去時間 // ///////////////////////////////////////////////////////////////////////// NSDate *futureDate = [NSDate distantFuture]; NSDate *pastDate = [NSDate distantPast]; ///////////////////////////////////////////////////////////////////////// // //(5)時間相加 // ///////////////////////////////////////////////////////////////////////// // 返回一個後天當前時刻物件(在明天基礎上再加上一天的時間) NSDate *theDayAfterTomorrow = [tomorrow dateByAddingTimeInterval:24*60*60]; ///////////////////////////////////////////////////////////////////////// // //(6)時間比較 // ///////////////////////////////////////////////////////////////////////// // 比較兩個時間物件是否相同返回布林值(由於精度問題,isTheSameDate 為 false) BOOL isTheSameDate = [theDayAfterTomorrow isEqualToDate:[[NSDate alloc] initWithTimeInterval:2*24*60*60 sinceDate:now]]; // 返回兩個時間中較早的一個時間 NSDate *earlierOne = [now earlierDate:tomorrow]; // 返回兩個時間中較晚的一個時間 NSDate *laterOne = [now laterDate:tomorrow]; // 比較兩個時間物件是否相同並返回 NSComparisonResult 值 NSComparisonResult *comparisonResult = [now compare:tomorrow];
2、日期轉換(NSDateFormatter)
///////////////////////////////////////////////////////////////////////// // //(1)方式1:用已有日期格式進行轉換 // ///////////////////////////////////////////////////////////////////////// NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateStyle = NSDateFormatterMediumStyle; dateFormatter.timeStyle = NSDateFormatterMediumStyle; NSDate *now = [[NSDate alloc] init]; // Date 轉 String NSString *nowString = [dateFormatter stringFromDate:now]; // String 轉 Date now = [dateFormatter dateFromString:nowString]; ///////////////////////////////////////////////////////////////////////// // //(2)方式2:自定義日期格式進行轉換 // ///////////////////////////////////////////////////////////////////////// dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; // Date 轉 String nowString = [dateFormatter stringFromDate:now]; // String 轉 Date now = [dateFormatter dateFromString:nowString];
3、工具類
/** * 將 NSDate 轉換成 NSString 格式 * * @param date * @param format 日期格式化樣式字串,如:2015-10-10 16:00:00 * * @return */ - (NSString *)stringFromDate:(NSDate *)date format:(NSString *)format { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = format; return [dateFormatter stringFromDate:date]; } /** * 將 NSString 轉換成 NSDate 格式 * * @param dateString * @param format 日期格式化樣式字串,如:2015-10-10 16:00:00 * * @return */ - (NSDate *)dateFromString:(NSString *)dateString format:(NSString *)format { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = format; return [dateFormatter dateFromString:dateString]; }