【问题标题】:Date and Time Format in iOS applicationiOS 应用程序中的日期和时间格式
【发布时间】:2015-12-23 05:41:25
【问题描述】:

我收到的回复低于日期格式

2015-12-23 05:17:04

但是,我想检查日期和时间,例如,

日期 == 今天? 日期 == 昨天?

时间 == 1 小时 时间 == 2 小时到 9 小时

怎么做?

【问题讨论】:

标签: ios objective-c nsdate nsdateformatter


【解决方案1】:

要检查日期是否是今天、昨天、明天等...您可以使用NSDate-Extensions

例如:

NSDate *date = [self convertStringToDate:@"2015-12-24 12:40:04" formate:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"%d", [date isToday]);
NSLog(@"%d", [date isTomorrow]);
NSLog(@"%d", [date isYesterday]);

- (NSDate *)convertStringToDate: (NSString *)date formate:(NSString *)formate {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [dateFormat setDateFormat:formate];
    return [dateFormat dateFromString:date];
}

【讨论】:

    【解决方案2】:

    使用这个方法给你 NSString 回来。您可以在方法中比较字符串或时间。

    -(NSString* )AgoStringFromTime:(NSDate*)dateTime
    {
        NSDictionary *timeScale = @{@"sec"  :@1,
                                    @"min"  :@60,
                                    @"hr"   :@3600,
                                    @"day"  :@86400,
                                    @"week" :@605800,
                                    @"month":@2629743,
                                    @"year" :@31556926};
        NSString *scale;
        int timeAgo = 0-(int)[dateTime timeIntervalSinceNow];
    
        if (timeAgo < 60) {
            scale = @"sec";
        } else if (timeAgo < 3600) {
            scale = @"min";
        } else if (timeAgo < 86400) {
            scale = @"hr";
        } else if (timeAgo < 605800) {
            scale = @"day";
        } else if (timeAgo < 2629743) {
            scale = @"week";
        } else if (timeAgo < 31556926) {
            scale = @"month";
        } else {
            scale = @"year";
        }
    
        timeAgo = timeAgo/[[timeScale objectForKey:scale] integerValue];
        NSString *s = @"";
        if (timeAgo > 1) {
            s = @"s";
        }
    
        return [NSString stringWithFormat:@"%d %@%@", timeAgo, scale, s];
    }
    

    【讨论】:

    • 这个答案是我想要的完美,但它只返回今天、昨天和休息......但我想像昨天上午 9:50 一样
    • @Florence 听起来不错。昨天,如果方法返回“day”,您可以输入逻辑。您可以使用 NSDate+NVTimerAgo 库,希望对您有所帮助。
    • @佛罗伦萨。谢谢。
    【解决方案3】:

    NSDate *date = [NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    dateFormatter.dateFormat = [NSString stringWithFormat:@"yyyyMMddHHmmss"];
    
    NSString *now = [dateFormatter stringFromDate:date];

    你可以试试上面的代码

    祝你好运

    【讨论】:

      【解决方案4】:

      1.首先您可以将字符串格式化为日期,如下所示:

      + (NSDate *)dateFromString:(NSString *)dateString {
      if (!dateString) {
          return nil;
      }
      
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
      [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
      [formatter setLocale:[NSLocale currentLocale]];
      NSDate *date= [formatter dateFromString:dateString];
      
      return date;
      }
      

      2.那么,你可以使用NSDate的方法,

      + (instancetype)date;//get current
      - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;//Returns the interval between the receiver and another given date.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-13
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-17
        相关资源
        最近更新 更多