当前时间转字符串(date→string)
-
Mon Dec 7 11:25:11 2020
→ 2020-12-07 11:25:11
- (NSString *)getNowDateFormatString{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//格式可自定义;
//DD为全年的中的第n天,dd为某月中的第n天;
//HH为24小时时间制,hh为12小时时间制;
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *nowDate = [NSDate date];
NSString *timeString = [formatter stringFromDate:nowDate];
return timeString;
}
当前时间转时间戳(date→integer)
-
Mon Dec 7 11:52:20 2020
→ 1607313140000
- (NSInteger)getNowDateFormatInteger{
NSDate *datenow = [NSDate date];
NSInteger timeStamp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
// iOS默认精度为妙(10位),通常server定义的精度为毫秒,乘以1000即可;
NSInteger timeInteger = timeStamp * 1000;
return timeInteger;
}
时间戳转字符串(integer→string)
-
1607313140000
→ 2020-12-07 11:25:11
- (NSString *)formatTimeWithTimeStamp:(NSInteger)integer{
//这里以13位时间戳为例,ios默认精度为妙,故除以1000后再转换;
//如果这里传入的integer有误,则会返回时间起始年"1970";
NSInteger target = integer / 1000;
NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:target];
//格式化成目标时间格式
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *timeString = [formatter stringFromDate:date];
return timeString;
}
格式化统一字符串形式(统一样式,统一成其中的一种)
2019/07/26 10:32:18
2019-07-26 10:32:18
- (NSString *)dateFormatWithString:(NSString *)string {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 传进来的日期格式
[formatter setDateFormat:@"YYYY/MM/dd HH:mm:ss"];
// 先将传进来的字符串日期转换成NSDate
NSDate *date =[formatter dateFromString:string];
// 目标日期格式
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *timeString = [formatter stringFromDate:date ];
return timeString;
}
天数差(与当前时间做比较,相差的天数)
- (NSInteger)daysOfNumberWithString:(NSString *)lastString{
//这里需要注意传入参数lastString时间格式,要与当前时间格式统一,才可以作比较
NSInteger days;
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
//这里格式要设置成统一
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *lastDate = [formatter dateFromString:laststring];
//调用上述时间格式化方法
NSString *nowString = [self getNowDateFormatString];
NSDate *nowDate = [formatter dateFromString:nowString];
NSTimeInterval time = [nowDate timeIntervalSinceDate:lastDate];
days = time / (60 * 60 *24);
return days;
}
多少天多少小时多少分钟多少秒(例如朋友圈发布的消息,已经过了多长时间
)
- (NSString *)dateTimeDifferenceWithStartTime:(NSString *)startTime endTime:(NSString *)endTime {
NSDateFormatter *date = [[NSDateFormatter alloc]init];
[date setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
NSDate *startDate =[date dateFromString:startTime];
NSDate *endDdate = [date dateFromString:endTime];
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *dateComponents = [cal components:unitFlags fromDate:startDate toDate:endDdate options:0];
// 天
NSInteger day = [dateComponents day];
// 小时
NSInteger house = [dateComponents hour];
// 分
NSInteger minute = [dateComponents minute];
// 秒
NSInteger second = [dateComponents second];
NSString *timeStr;
if (day != 0) {
timeStr = [NSString stringWithFormat:@"%zd天%zd小时%zd分%zd秒",day,house,minute,second];
}
else if (day==0 && house !=0) {
timeStr = [NSString stringWithFormat:@"%zd小时%zd分%zd秒",house,minute,second];
}
else if (day==0 && house==0 && minute!=0) {
timeStr = [NSString stringWithFormat:@"%zd分%zd秒",minute,second];
}
else{
timeStr = [NSString stringWithFormat:@"%zd秒",second];
}
return timeStr;
}