iOS时间格式化、时间戳、日期格式化、常见格式互转

1)当前时间格式化---转字符串
- (NSString *)getNowDateFormatString

{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //格式可自定义
    [formattersetDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    NSDate   *datenow = [NSDatedate];

    NSString *timeimeString = [formatterstringFromDate:datenow];

    return   timeimeString;

}
2)当前时间格式化---转时间戳(13位)
-(NSString *)getNowDateTimeInteger{
     NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    //这里可以设置时区,达到两种时间0误差
    //NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    //[formatter setTimeZone:timeZone];
    NSDate *datenow = [NSDate date];
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];

    //时间戳的值,iOS默认精度是10位,android默认是13位
    NSInteger t_integer = timeSp *1000;
    NSString  *t_tringt= [NSString stringWithFormat:@"%ld",t_integer];
    return    t_tringt;
}
3)时间戳(13位)---转字符串
- (NSString *)stringTimeWithTimeString:(NSString *)string{
    NSInteger t_integer = [string integerValue] / 1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:t_integer];

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //格式化成目标时间格式
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
    NSString   *timeString  = [formatter stringFromDate:date];

    return     timeString;
}
4)统一格式化字符串形式(两种不同格式的时间,统一样式)
  • 例①“2019-07-26 10:32”
  • 例②“2019.07.26 10:32”
-(NSString *)dateWithFormatString:(NSString *)string
{
    // 实例化NSDateFormatter
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    //传进来的日期格式
    [formatter1 setDateFormat:@"yyyy-MM-dd HH:mm"];
    //要转换的日期字符串
    NSString *dateString1 = string;
    //NSDate形式的日期
    NSDate *date =[formatter1 dateFromString:dateString1];

    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    //目标格式
    [formatter2 setDateFormat:@"yyyy.MM.dd HH:mm"];
    NSString *timeString = [formatter2 stringFromDate:date ];

    return timeString;
}
5)天数差(与当前时间做比较,相差的天数)
- (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;
}
6)多少天多少小时多少分钟多少秒
  • 例如朋友圈发布的消息,已经过了多长时间
- (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;
 }

你可能感兴趣的:(iOS时间格式化、时间戳、日期格式化、常见格式互转)