获取系统当前的时间
- yyy --代表 年(2017)
- MM --代表 月 (06)
- dd -- 代表 日 (16)
- HH --代表 时 (13)
- mm --代表 分 (22)
- ss -- 代表秒 (14)
- yyy-MM-dd HH:mm:ss -----> 2017-06-16 13:22:14
/**
* 获取当前系统时间
@return 字符串
*/
-(NSString*)getCurrentTime {
NSDateFormatter*formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
NSString*dateTime = [formatter stringFromDate:[NSDate date]];
NSLog(@"当前时间是===%@",dateTime);
// 当前时间是===2017-06-16 13:22:14
return dateTime;
}
获取当前时间戳
-(NSString*)getCurrentTimestamp{
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
return timeString;
}
把时间戳转化为时间
-(NSString*)SetTime:(NSString*)time{
NSDateFormatter*formatter = [[NSDateFormatter alloc]init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyMMdd"];
int timeval = [time intValue];
NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeval];
// NSLog(@"1296035591 = %@",confromTimesp);
NSString*confromTimespStr = [formatter stringFromDate:confromTimesp];
return confromTimespStr;
}
时间差转换
ZBHomeDoctorChat *chatPre = _dataSource[indexPath.row - 1];
ZBHomeDoctorChat *chatNow = _dataSource[indexPath.row];
NSTimeInterval time1 = [chatPre.dialog_timestamp doubleValue];
NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:time1];
NSTimeInterval time2 = [chatNow.dialog_timestamp doubleValue];
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:time2];
NSLog(@"date1---%@*********date2*****%@",date1,date2);
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *d = [cal components:unitFlags fromDate:date1 toDate:date2 options:0];
//总共有多少秒
long sec = [d hour]*3600+[d minute]*60+[d second];
NSLog(@"%ld小时 %ld分钟 %ld秒",[d hour],[d minute],[d second]);
2017-04-25 15:17:48.731172 FamilyPlatForm[19475:8863564] date1---2017-04-25 04:26:10 +0000*********date2*****2017-04-25 04:28:54 +0000
2017-04-25 15:17:48.731416 FamilyPlatForm[19475:8863564] 0小时 2分钟 44秒