• 字符串时间转成,昨天,今天,明天格式


    //获取 字符串

    1.

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

        [outputFormatter setLocale:[NSLocale currentLocale]];

        [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

        NSDate *strDate = [outputFormatter dateFromString:image.beginTime];

        //修正8小时的差时

        NSTimeZone *zone = [NSTimeZone systemTimeZone];

        NSInteger interval = [zone secondsFromGMTForDate: strDate];

        NSDate *endDate = [strDate  dateByAddingTimeInterval: interval];

        NSString *lastTime = [self compareDate:endDate];

    //给字符串所在的控件赋值,一般在setmodel中赋值显示

       self.timelabel.text = [NSString stringWithFormat:@"%@开播",lastTime];

    //调用方法compareDate方法

    -(NSString *)compareDate:(NSDate *)date{

        

        NSTimeInterval secondsPerDay = 24 * 60 * 60;

        

        //修正8小时之差

        NSDate *date1 = [NSDate date];

        NSTimeZone *zone = [NSTimeZone systemTimeZone];

        NSInteger interval = [zone secondsFromGMTForDate: date1];

        NSDate *localeDate = [date1  dateByAddingTimeInterval: interval];

        

        //NSLog(@"nowdate=%@ olddate = %@",localeDate,date);

        NSDate *today = localeDate;

        NSDate *yesterday,*beforeOfYesterday;

        //今年

        NSString *toYears;

        

        toYears = [[today description] substringToIndex:4];

        

        yesterday = [today dateByAddingTimeInterval: secondsPerDay];

        beforeOfYesterday = [yesterday dateByAddingTimeInterval: secondsPerDay];

        

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

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

        NSString *beforeOfYesterdayString = [[beforeOfYesterday description] substringToIndex:10];

        

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

        NSString *dateYears = [[date description] substringToIndex:4];

        

        NSString *dateContent;

        if ([dateYears isEqualToString:toYears]) {//同一年

            //今 昨 前天的时间

            NSString *time = [[date description] substringWithRange:(NSRange){11,5}];

            //其他时间

            NSString *time2 = [[date description] substringWithRange:(NSRange){5,11}];

            if ([dateString isEqualToString:todayString]){

                dateContent = [NSString stringWithFormat:@"今天 %@",time];

                return dateContent;

            } else if ([dateString isEqualToString:yesterdayString]){

                dateContent = [NSString stringWithFormat:@"明天 %@",time];

                return dateContent;

            }else if ([dateString isEqualToString:beforeOfYesterdayString]){

                dateContent = [NSString stringWithFormat:@"前天 %@",time];

                return dateContent;

            }else{

                return time2;

            }

        }else{

            return dateString;

        }

    }

  • 相关阅读:
    Smarty数据、模版创建指引
    做了个google工具栏的饭否按钮
    php程序调试(远程调试,firephp)
    Linux shell编程与Makefile的一些记录(等待更新)
    My MSSQL Tips
    Castle.ActiveRecord中Save与SaveAndFlush的性能差别
    Linux Basic Approach (My Notes)
    去青城后山玩了一天
    Infopath 2007 使用实践(占位)
    Delphi 6
  • 原文地址:https://www.cnblogs.com/whx060900/p/10266926.html
Copyright © 2020-2023  润新知