#pragma mark - 将某个时间戳转化成 时间 - (NSString *)getDateStringWithTimeStr:(NSString *)str{ NSTimeInterval time=[str doubleValue]/1000;//传入的时间戳str如果是精确到毫秒的记得要/1000 NSDate *detailDate=[NSDate dateWithTimeIntervalSince1970:time]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //实例化一个NSDateFormatter对象 //设定时间格式,这里可以设置成自己需要的格式 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *currentDateStr = [dateFormatter stringFromDate: detailDate]; return currentDateStr; } #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); }
// 发布 1天转换成时间戳 距离1970多少秒 NSDate *newDate = [nowDate dateByAddingTimeInterval:60 * 60 * 24 * ([_setDaysTextField.text floatValue])]; long firstStamp = [newDate timeIntervalSince1970]; // 时间戳转换成日期 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:firstStamp]; NSLog(@"1296035591 = %@",confromTimesp);