一、字符串轉(zhuǎn)化成日期:
NSString *str = @"2017-10-21 23:00:11 +0800";
// 1.創(chuàng)建一個(gè)時(shí)間格式化對(duì)象
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 2.格式化對(duì)象的樣式
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
// 3.格式化讓字符串轉(zhuǎn)換成時(shí)間
NSDate *datestr = [formatter dateFromString:str];
// 4.自己給定的時(shí)間
NSDate *currentDate = datestr;
二、獲取當(dāng)月天數(shù):
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//此處是查看當(dāng)前月的天數(shù)
NSDate *timeDate = [NSDate date];
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit: NSMonthCalendarUnit forDate:timeDate];
NSInteger countInteger = range.length;
NSLog(@"當(dāng)月天數(shù)為:%lu",(unsigned long)countInteger);
三、獲取指定日期是周幾
NSDate *datestr = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日歷的算法
NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:datestr];
// 1:周日,2:周一 ,3.周二 ? ? ...以此類推
NSLog(@"今天是:%@",@([comps weekday]));
四、判斷指定時(shí)間與當(dāng)前時(shí)間大小
// 時(shí)間字符串
NSString *starTime= @"2017-23-20 11:10:05";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *currentTime = [format dateFromString:starTime];
// 當(dāng)前時(shí)間
NSDate *nowTime = [NSDate date];
// 獲得比較結(jié)果(誰大誰小)
NSComparisonResult result = [nowTime compare:currentTime];
if (result == NSOrderedAscending) {
// 升序, 越往右邊越大
NSLog(@"創(chuàng)建時(shí)間大于當(dāng)前時(shí)間");
} else if (result == NSOrderedDescending) {
// 降序, 越往右邊越小
NSLog(@"創(chuàng)建時(shí)間小于當(dāng)前時(shí)間");
} else {
NSLog(@"創(chuàng)建時(shí)間等于當(dāng)前時(shí)間");
}
五、獲取年月日星期時(shí)分秒
NSDate* date? = [NSDate date];
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
?// NSDateComponent 可以獲得日期的詳細(xì)信息,即日期的組成
NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:date];
? ? NSLog(@"年 = year = %ld",comps.year);
? ? NSLog(@"月 = month = %ld",comps.month);
? ? NSLog(@"日 = day = %ld",comps.day);
? ? NSLog(@"時(shí) = hour = %ld",comps.hour);
? ? NSLog(@"分 = minute = %ld",comps.minute);
? ? NSLog(@"秒 = second = %ld",comps.second);
? ? NSLog(@"星期 =weekDay = %ld ",comps.weekday);
//字符串類型? 轉(zhuǎn)化為? 時(shí)間
NSString *string = @"1481706995000";
NSTimeInterval time = [string doubleValue] ;
NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time/1000.0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSLog(@"轉(zhuǎn)換的時(shí)間為:%@",[dateFormatter stringFromDate:date]);

輸出:
轉(zhuǎn)換的時(shí)間為:2016-12-14 17:56:35
記錄兩次請(qǐng)求前后時(shí)間差:
NSDate? *date1 = [NSDate date];
NSDate? *date2 = [NSDate date];
NSTimeInterval? a = [date2? timeIntervalSince1970]? -? [date1? timeIntervalSince1970];
NSLog(@"A與B之間相差%.3f秒",a);
時(shí)間戳? 時(shí)間 互換
//時(shí)間戳 -> 時(shí)間
NSDateFormatter *tmpFormatter = [[NSDateFormatter alloc] init];
[tmpFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
[tmpFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
NSDate *tmpTimedate = [NSDate dateWithTimeIntervalSince1970:tmpTime];
_mStrTime = [tmpFormatter stringFromDate:tmpTimedate];
參考:
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss")----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSLog(@"1296035591? = %@",confromTimesp);
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
//NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);
return confromTimespStr;
}