1。//获取当前时间戳
- (NSInteger )getCurrentTimeStamp {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
//设置时区,这个对于时间的处理有时很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];//现在时间
//时间转时间戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
return timeSp;
}
2。//获取两个时间戳的相差时间
-(Nsstring)getDateTimeWithLastTime:(Nsinteger )lastTime NowTime:(Nsinteger)nowTime {
NSTimeInterval value = nowTime - lastTime;
int second = (int)value %60;//秒
int minute = (int)value /60%60;
int house = (int)value / (24 *3600)%3600;//时
int day = (int)value / (24 *3600);
NSString *str;
if (day != 0) {
str = [NSString stringWithFormat:@"耗时%d天%d小时%d分%d秒",day,house,minute,second];
}else if (day==0 && house !=0) {
str = [NSString stringWithFormat:@"耗时%d小时%d分%d秒",house,minute,second];
}else if (day==0 && house==0 && minute!=0) {
str = [NSString stringWithFormat:@"耗时%d分%d秒",minute,second];
}else{
str = [NSString stringWithFormat:@"耗时%d秒",second];
}
NSLog(@" %@ ",str);
return str;
}