美文网首页
NSDate实用总结

NSDate实用总结

作者: SlimMan | 来源:发表于2015-09-06 15:24 被阅读563次
    • 将当前对时间显示出来
      NSDate -> NSSTring
      NSDate *date = [NSDate date];
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"yyyy/MM/dd hh:mm:ss"];
      self.timeLabel.text = [dateFormatter stringFromDate:date];
      NSSTring -> NSDate
      NSString *dateString = self.timeLabel.text;
      NSDate *nowDate = [dateFormatter dateFromString:dateString];
      self.timeLabel2.text = [dateFormatter stringFromDate:nowDate];

    • 分别获取指定日期的年、月、日、时、分、秒
      [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
      NSString *dateString = @"2016/10/12 3:2:34";
      NSDate *theDate = [dateFormatter dateFromString:dateString];
      NSDateComponents *com = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:theDate];
      NSInteger year = com.year;
      NSInteger month = com.month;
      NSInteger day = com.day;
      NSInteger hour = com.hour;
      NSInteger minute = com.minute;
      NSInteger second = com.second;
      self.timeLabel2.text = [NSString stringWithFormat:@"%ld年%ld月%ld日 %02ld时:%02ld分:%02ld秒, year, month, day, hour, minute, second];
      上面的代码写法比较快,但是不够清楚,下面换种写法,感觉你就是大牛有木有
      NSString *dateString = @"2015/09/06 3:2:34";
      NSDate *theDate = [dateFormatter dateFromString:dateString];
      NSCalendar *calendar = [NSCalendar currentCalendar];
      NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
      NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:theDate];
      NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];

        // 2.格式化日期
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        if ([cmp1 day] == [cmp2 day]) { // 今天
            formatter.dateFormat = @"今天 HH:mm";
        } else if ([cmp1 year] == [cmp2 year]) { // 今年
            formatter.dateFormat = @"MM-dd HH:mm";
         } else {
            formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        }
        NSString *time = [formatter stringFromDate:theDate];
        
        // 3.显示时间
        self.timeLabel2.text = time;
      

    注意,通过改变NSDateFormatter的dateFormat表现形式可以实现各种你想要的时间表示形式,比如
    NSString *dateString = @"2015/09/08 3:2:34";
    NSDate *theDate = [dateFormatter dateFromString:dateString];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"'日期:'yyyy有鬼MM😊dd'时间:'HH好玩吗mm🐎ss";
    self.timeLabel2.text = [formatter stringFromDate:theDate];
    另外提到的一点是:要获取那一天是星期几的话把CalendarUnitWeekday加入就好了。

    • 获取指定日期的月份有多少天
      NSString *dateString = @"2015/10/08 3:2:34";
      NSDate *theDate = [dateFormatter dateFromString:dateString];
      NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
      NSRange MonthRange = [gregorian rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:theDate];
      NSInteger sumDays = MonthRange.length;
      self.timeLabel2.text = [NSString stringWithFormat:@"%ld天", sumDays];
      获取到了一个月份有多少天后,自然就可以想到下一个功能就是准确地从指定日期月份得到指定月份的下一个月份的第一天,这个功能常被用于日历的生成
      NSString *dateString = @"2015/10/08 3:2:34";
      NSDate *theDate = [dateFormatter dateFromString:dateString];
      NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
      NSRange MonthRange = [gregorian rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:theDate];
      NSInteger sumDays = MonthRange.length;
      NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:theDate];
      NSInteger day = components.day;
      NSDate *firstDate = [theDate dateByAddingTimeInterval:60 * 60 * 24 * (sumDays - day + 1)];
      self.timeLabel2.text = [dateFormatter stringFromDate:firstDate];

    • 从1970神马的
      前端总是少不了要和后台进行交互,然后后台给我们日期的方式总是一个什么从1970年到那个日期的毫秒数(ps:谁能告诉我为什么是1970年)然后我们就不得不把它们转过来转过去了
      NSDate -> NSTimeInterval
      NSString *dateString = @"2015/10/08 3:2:34";
      NSDate *theDate = [dateFormatter dateFromString:dateString];
      NSTimeInterval timeInter = [theDate timeIntervalSince1970] * 1000;
      self.timeLabel2.text = [NSString stringWithFormat:@"1970年到%@的毫秒数为:%.2f", dateString, timeInter];

      NSTimeInterval -> NSDate
      NSString *dateString = @"2015/10/08 3:2:34";
      NSDate *theDate = [dateFormatter dateFromString:dateString];
      NSTimeInterval timeInter = [theDate timeIntervalSince1970] * 1000;
      NSDate *sameDate = [NSDate dateWithTimeIntervalSince1970:timeInter/1000];
      self.timeLabel2.text = [dateFormatter stringFromDate:sameDate];

    • 格式化输出 年月日 上下午 时分秒 星期几
      显示效果如下:



      代码如下:
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
      formatter.dateFormat = @"yyyy/MM/dd HH🐷mm🐎ss";
      NSString *dateString = @"2015/09/08 3:2:34";
      NSDate *theDate = [formatter dateFromString:dateString];

        formatter.dateFormat = @"MM月dd日 a HH:mm EEEE";
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 50)];
        label.text = [formatter stringFromDate:theDate]; 
        [self.view addSubview:label];
      
    • 判断某一天是不是昨天、今天、明天、周末等
      代码如下:
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
      formatter.dateFormat = @"yyyy/MM/dd HH:mm:ss";
      NSString *dateString = @"2015/09/20 3:2:34";
      NSDate *theDate = [formatter dateFromString:dateString];
      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 50)];
      label.text = [formatter stringFromDate:theDate];
      [self.view addSubview:label];

        NSCalendar *calendar = [NSCalendar currentCalendar];
        BOOL isToday     = [calendar isDateInToday:theDate];
        BOOL isYesterday = [calendar isDateInYesterday:theDate];
        BOOL isTomorrow  = [calendar isDateInTomorrow:theDate];
        BOOL isInWeek    = [calendar isDateInWeekend:theDate];
        
        NSString *labelString = @"";
        if (isInWeek) {
            labelString = @"周末";
        }
        
        if (isYesterday) {
            labelString = @"昨天";
        }
        
        if (isTomorrow) {
            labelString = @"明天";
        }
        
        if (isToday) {
            labelString = @"今天";
        }
        
        label.text = labelString;
      

    相关文章

      网友评论

          本文标题:NSDate实用总结

          本文链接:https://www.haomeiwen.com/subject/hjhrcttx.html