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

iOS-24小时进制时、分、秒字符串转为时、分字符串,第1张

最近遇到一个坑,后台返回的时间格式是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"

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/996832.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存