zoukankan      html  css  js  c++  java
  • 时间与NSString转换

    //传进来时间字符串转换

    + (NSString *) getChatTimeString:(NSString *)timeString {

        if (timeString==nil || timeString.length==0 || [timeString isEqualToString:@"0"]||timeString.length < 10) {

    #warning XXX

            return @"";

        }

    /**将字符串转换成时间*/

        NSDate *timeDate = [CommonFunction getCurrentStringAsDate:timeString];

        NSTimeInterval secondsPerDay = 24 * 60 * 60;

        NSDate *date = [NSDate date];

        NSDate *qian = [date dateByAddingTimeInterval: - 2 * secondsPerDay];

        NSDate *yesterday = [date dateByAddingTimeInterval: -secondsPerDay];

        

        NSString * todayString = [[date description] substringToIndex:10];

        NSString * yesterdayString = [[yesterday description] substringToIndex:10];

        NSString * qianString = [[qian description] substringToIndex:10];

        NSString * dateString = [[timeDate description] substringToIndex:10];

        

        NSString *year1 = [todayString substringWithRange:NSMakeRange(0, 4)];

        NSString *year2 = [timeString substringWithRange:NSMakeRange(0, 4)];

        if ([dateString isEqualToString:todayString]&& timeString.length>=16)//是否等于今天

        {

    //        return [timeString substringWithRange:NSMakeRange(11, 5)];

            return [NSString stringWithFormat:@"今天 %@",[timeString substringWithRange:NSMakeRange(11, 5)]];

        } else if ([dateString isEqualToString:yesterdayString])//是否等于昨天

        {

            return [NSString stringWithFormat:@"昨天 %@",[timeString substringWithRange:NSMakeRange(11, 5)]];

        }else if ([dateString isEqualToString:qianString])//是否等于前天

        {

            return [NSString stringWithFormat:@"前天 %@",[timeString substringWithRange:NSMakeRange(11, 5)]];

        }

        else

        {

            if (![year1 isEqualToString:year2]) {

                return timeString;

            }

            return [timeString substringWithRange: NSMakeRange(5, 11)];

        }

    //系统时间转换 

    + (NSDate *) getCurrentStringAsDate:(NSString *)date {

         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        [dateFormatter setDateFormat: @"YYYY-MM-dd HH:mm"];

        NSDate *dateString = [dateFormatter dateFromString:date];

        return dateString;

    }

    //获取当前时间日期,返回字符串

    + (NSString *) getCurrentDateAsString {

         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        [dateFormatter setDateFormat: @"YYYY-MM-dd HH:mm:ss"];

        NSString *dateString = [dateFormatter stringFromDate: [NSDate date]];

        return dateString;

    }

    将来的自己,会感谢现在不放弃的自己!
  • 相关阅读:
    Java基础--day04
    Java基础--day03
    Java基础--day02
    高斯键盘设置指南
    博客园主题配置
    算法笔记--二分
    Test2反思
    树链剖分【模板】
    7.20关于莫队算法的一些初步理解
    分块(n根n复杂度)
  • 原文地址:https://www.cnblogs.com/TheYouth/p/4981314.html
Copyright © 2011-2022 走看看