iOS-24小时进制时、分、秒字符串转为时、分字符串

最近遇到一个坑,后台返回的时间格式是24小时进制的字符串 @"2021-05-07 16:43:49",但是需求需要格式化一下去掉秒,展示时、分;按照如下代码进行,发现转换出来的字符串是空,其实主要是转化后的日期为nil导致的

//日期转为 时-分
+ (NSString *)dateFormatStringToMinute:(NSString *)dateStr {
    /*----当前时间转为时分----*/
    // 实例化NSDateFormatter
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置日期格式
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    // 获取当前日期
    NSDate *currentDate = [NSDate date];
    NSString *currentDateString = [formatter stringFromDate:currentDate];
    NSLog(@"%@", currentDateString);
    
    
    NSDateFormatter *dateFormaterMinute = [[NSDateFormatter alloc] init];
    dateFormaterMinute.timeZone = [NSTimeZone systemTimeZone];
    dateFormaterMinute.locale = [NSLocale autoupdatingCurrentLocale];
    
//    [dateFormaterMinute setTimeZone:[NSTimeZone localTimeZone]];
//    //指定语言
//    [dateFormaterMinute setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    
    //这里如果不设置为UTC时区,会把要转换的时间字符串定为当前时区的时间(东八区)转换为UTC时区的时间
    [dateFormaterMinute setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    if ([dateStr rangeOfString:@"00:00:00"].location != NSNotFound) {
        [dateFormaterMinute setDateFormat:@"yyyy-MM-dd"];
    }else {
        [dateFormaterMinute setDateFormat:@"yyyy-MM-dd HH:mm"]; //采用年月日 时分格式
    }
    
    NSDate *formateDate = [dateFormaterMinute dateFromString:dateStr];
    if (!formateDate) { // 区分是不是日期字符串,转化失败,则不是
        return dateStr;
    }
    
    // NSDate ->NSString
    dateFormaterMinute.dateFormat = dateStr;
    // 指定语言
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
    [dateFormaterMinute setLocale:locale];
    NSString *resultStr = [dateFormaterMinute stringFromDate:formateDate];
    return resultStr;
}

输出结果如下:

但是获取当前日期按照上面格式化不会为nil,如下:

按照网上各种设置时区,语言都不顶用,最后博主改变思路,多格式化一次,先将后台返回的时、分、秒字符转化成时、分、秒日期类型,再将时、分、秒日期类型格式化成时、分字符串即可

//日期转为 时-分
+ (NSString *)dateFormatStringToMinute:(NSString *)dateStr {
    if (dateStr == nil || [dateStr isEqual:[NSNull null]] || [dateStr isEqualToString:@""]) {
        return @"";
    }

    NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
    if ([dateStr rangeOfString:@"00:00:00"].location != NSNotFound) {
        [dateFormater setDateFormat:@"yyyy-MM-dd"];
    }else {
        [dateFormater setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //采用年月日 时分格式
    }
    NSDate *date = [dateFormater dateFromString:dateStr];
//    dateStr = [dateFormater stringFromDate:date];
    
    NSDateFormatter *dateFormaterMinute = [[NSDateFormatter alloc] init];
//    dateFormater.timeZone = [NSTimeZone systemTimeZone];
//    dateFormater.locale = [NSLocale autoupdatingCurrentLocale];
    
//    [fmt setTimeZone:[NSTimeZone localTimeZone]];
//    [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    // 指定语言
//    [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    
    // 这里如果不设置为UTC时区,会把要转换的时间字符串定为当前时区的时间(东八区)转换为UTC时区的时间
//    [dateFormaterMinute setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    if ([dateStr rangeOfString:@"00:00:00"].location != NSNotFound) {
        [dateFormaterMinute setDateFormat:@"yyyy-MM-dd"];
    }else {
        [dateFormaterMinute setDateFormat:@"yyyy-MM-dd HH:mm"]; //采用年月日 时分格式
    }
    
//    NSDate *formateDate = [dateFormaterMinute dateFromString:dateStr];
//    if (!formateDate) { // 区分是不是日期字符串,转化失败,则不是
//        return dateStr;
//    }
    
//    // NSDate ->NSString
//    dateFormaterMinute.dateFormat = dateStr;
    // 指定语言
//    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
//    [fmt setLocale:locale];
    NSString *resultStr = [dateFormaterMinute stringFromDate:date];
    return resultStr;
}

这时再看结果resultStr就是格式化之后的时、分字符串:@"2021-05-07 16:43"

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值