NSDate转换为时间戳
+(NSString *)dateChangeToTimestamp:(NSDate *)date{
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
return timeSp;
}
时间戳转换为NSDate
+(NSDate *)timestampChangeToDate:(double)timestamp{
return [NSDate dateWithTimeIntervalSince1970:timestamp];
}
时间戳转换为NSString
+(NSString *)timestampChangeToDateString:(double)timestamp{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateString = [dateformatter stringFromDate:date];
return dateString;
}
时间戳转化为需要自定义显示的格式
+(NSString *)getTimeWithTimestamp:(double)timestamp{
NSDate * date = [NSString timestampChangeToDate:timestamp];
NSDate * now = [NSDate date];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
if ([[formatter stringFromDate:now] intValue] > [[formatter stringFromDate:date] intValue]) {
[formatter setDateFormat:@"yyyy-MM-dd"];
return [formatter stringFromDate:date];
}
[formatter setDateFormat:@"MM"];
if ([[formatter stringFromDate:now] intValue] > [[formatter stringFromDate:date] intValue]) {
[formatter setDateFormat:@"MM-dd HH:mm"];
return [formatter stringFromDate:date];
}
[formatter setDateFormat:@"dd"];
int days = [[formatter stringFromDate:now] intValue] - [[formatter stringFromDate:date] intValue];
if (days >= 2) {
[formatter setDateFormat:@"MM-dd HH:mm"];
return [formatter stringFromDate:date];
}
else if (days >= 1){
[formatter setDateFormat:@"HH:mm"];
return [NSString stringWithFormat:@"昨天%@",[formatter stringFromDate:date]];
}
else {
[formatter setDateFormat:@"HH:mm"];
return [NSString stringWithFormat:@"今天%@",[formatter stringFromDate:date]];
}
return @"";
}
时间差转换为视频时长的显示格式:
+(NSString *)videoRecordTime:(int)recordTime{
if (recordTime < 10) {
return [NSString stringWithFormat:@"0:0%d",recordTime];
}else if (recordTime < 60){
return [NSString stringWithFormat:@"0:%d",recordTime];
}else{
if (recordTime%60 < 10) {
return [NSString stringWithFormat:@"%d:0%d",recordTime/60,recordTime%60];
}else{
return [NSString stringWithFormat:@"%d:%d",recordTime/60,recordTime%60];
}
}
}
判断某个时间是否在某个时间段之间
+(BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour {
NSDate *dateFrom = [NSString getCustomDateWithHour:fromHour];
NSDate *dateTo = [NSString getCustomDateWithHour:toHour];
NSDate *currentDate = [NSDate date];
if ([currentDate compare:dateFrom]==NSOrderedDescending && [currentDate compare:dateTo]==NSOrderedAscending) {
return YES;
}
return NO;
}
+(NSDate *)getCustomDateWithHour:(NSInteger)hour {
NSDate *currentDate = [NSDate date];
NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *currentComps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
NSDateComponents *resultComps = [[NSDateComponents alloc] init];
[resultComps setYear:[currentComps year]];
[resultComps setMonth:[currentComps month]];
[resultComps setDay:[currentComps day]];
[resultComps setHour:hour];
NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
return [resultCalendar dateFromComponents:resultComps];
}
NSDate转换为年龄
+(NSString *)dateToAge:(NSDate *)bornDate{
NSDate *currentDate = [NSDate date];
NSTimeInterval time = [currentDate timeIntervalSinceDate:bornDate];
int age = ((int)time)/(3600*24*365);
return [NSString stringWithFormat:@"%d",age];
}