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;
}