#pragma mark - 将某个时间戳转化成 时间
-(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")----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
// 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;
}
#pragma mark - 将字符串日期转换成想要格式的 NSDate
-(void)stringSwitchdate{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate* date = [formatter dateFromString:[self timestampSwitchTime:1490354236 andFormatter:@"YYYY-MM-dd HH:mm:ss"]]; //------------将字符串按formatter转成nsdate
NSLog(@"%@",date);
}
#pragma mark - 两个日期的时间差 返回秒数
-(void)differentTime{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *date1 = [formatter dateFromString:@"2017-03-24 19:42:30"];
NSDate *date2 = [formatter dateFromString:@"2017-03-24 20:42:30"];
//返回秒数
NSTimeInterval aTimer = [date2 timeIntervalSinceDate:date1];
int hour = (int)(aTimer/3600);
int minute = (int)aTimer/60;
NSString *dural = [NSString stringWithFormat:@"%d时%d分%f秒", hour, minute,aTimer];
NSLog(@"%@",dural);
}