zoukankan      html  css  js  c++  java
  • 日期间隔

    根据指定日期与现在日期时间对比相差几周几月

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    #define knewsTimeFormat @"yyyyMMddHHmmss" //你要传过来日期的格式
    #define kLocaleIdentifier @"en_US"
     
    // 发布时间
    - (NSString *)newsTime:(NSString *)newsTimes
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = knewsTimeFormat;
        formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:kLocaleIdentifier];
         
        NSDate *date = [formatter dateFromString:newsTimes];
         
        NSDate *now = [NSDate date];
         
        // 比较帖子发布时间和当前时间
        NSTimeInterval interval = [now timeIntervalSinceDate:date];
         
        NSString *format;
        if (interval <= 60) {
            format = @"刚刚";
        } else if(interval <= 60*60){
            format = [NSString stringWithFormat:@"发布于前%.f分钟", interval/60];
        } else if(interval <= 60*60*24){
            format = [NSString stringWithFormat:@"发布于前%.f小时", interval/3600];
        } else if (interval <= 60*60*24*7){
            format = [NSString stringWithFormat:@"发布于前%d天", (int)interval/(60*60*24)];
        } else if (interval > 60*60*24*7 & interval <= 60*60*24*30 ){
            format = [NSString stringWithFormat:@"发布于前%d周", (int)interval/(60*60*24*7)];
        }else if(interval > 60*60*24*30 ){
            format = [NSString stringWithFormat:@"发布于前%d月", (int)interval/(60*60*24*30)];
        }
         
        formatter.dateFormat = format;
        return [formatter stringFromDate:date];
    }
  • 相关阅读:
    Eclipse Kepler安装WST Server Adapter后创建Server无Tomcat解决方法
    centos下Linux C语言MD5的使用
    解析JSON字符串
    切换view的动画
    设置菜单和工具条
    视图切换的几种方法
    scrollview 例子2
    UIScrollView
    iOS:翻页效果
    软件预构的艺术源码编译
  • 原文地址:https://www.cnblogs.com/ranger-jlu/p/3877696.html
Copyright © 2011-2022 走看看