獲取當前的date
NSDate *nowDate = [NSDate date];
複製程式碼
使用NSCalendar 計算詳細的時間
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit | //年
NSMonthCalendarUnit | //月份
NSDayCalendarUnit | //日
NSHourCalendarUnit | //小時
NSMinuteCalendarUnit | //分鐘
NSSecondCalendarUnit; // 秒
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:nowDate];
NSInteger year = [dateComponent year];
NSInteger month = [dateComponent month];
NSInteger day = [dateComponent day];
NSInteger hour = [dateComponent hour];
NSInteger minute = [dateComponent minute];
NSInteger second = [dateComponent second];
複製程式碼