12

我想在Objective C中返回带有字符串“今天”和“昨天”以及日期的日期对象。欢迎所有评论:

我有格式为@"yyyy-MM-dd HH:mm:ss"] 的日期,然后确定日期是今天还是昨天,如果是,则返回“(昨天|今天|日期)”格式化字符串.

4

6 回答 6

26

NSDateFormatter 可以做到这一点。但是,这不适用于自定义日期格式,但在大多数情况下,当您需要相对日期时,您会将它们呈现给用户,并且您不应该首先使用硬编码的日期格式。

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterMediumStyle;
df.dateStyle = NSDateFormatterShortStyle;
df.doesRelativeDateFormatting = YES;  // this enables relative dates like yesterday, today, tomorrow...

NSLog(@"%@", [df stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-48*60*60]]);
NSLog(@"%@", [df stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-24*60*60]]);
NSLog(@"%@", [df stringFromDate:[NSDate date]]);
NSLog(@"%@", [df stringFromDate:[NSDate dateWithTimeIntervalSinceNow:24*60*60]]);
NSLog(@"%@", [df stringFromDate:[NSDate dateWithTimeIntervalSinceNow:48*60*60]]);

这将打印:

2013-06-06 09:13:22.844 x 2[11732:c07] 6/4/13, 9:13:22 AM
2013-06-06 09:13:22.845 x 2[11732:c07] Yesterday, 9:13:22 AM
2013-06-06 09:13:22.845 x 2[11732:c07] Today, 9:13:22 AM
2013-06-06 09:13:22.846 x 2[11732:c07] Tomorrow, 9:13:22 AM
2013-06-06 09:13:22.846 x 2[11732:c07] 6/8/13, 9:13:22 AM

在具有德语语言环境的设备上,这将在第一个和最后一个日期打印“Vorgestern”(前天)和“Übermorgen”(后天)。

于 2013-06-06T07:14:20.633 回答
5

NSDateFormatters呢setDoesRelativeDateFormatting

指定接收方是否对日期组件使用诸如“今天”和“明天”之类的短语。

- (void)setDoesRelativeDateFormatting:(BOOL)b
设置参数 b = YES 以指定接收器应使用相对日期格式,否则为 NO。

看一看:NSDateFormatter 类参考

于 2013-06-06T07:14:35.620 回答
3

我希望这也对你有用:

    NSDate *date = somedate;
    NSInteger dayDiff = (int)[date timeIntervalSinceNow] / (60*60*24);
    NSDateComponents *componentsToday = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
    NSDateComponents *componentsDate = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:somedate];
    NSInteger day = [componentsToday day] - [componentsDate day];
    if (dayDiff == 0) {
        NSLog(@"Today");
    } else if (dayDiff == -1) {
        NSLog(@"Yesterday");
    } else if(dayDiff > -7 && dayDiff < -1) {
        NSLog(@"This week");
    } else if(dayDiff > -14 && dayDiff <= -7) {
        NSLog(@"Last week");
    } else if(dayDiff >= -60 && dayDiff <= -30) {
        NSLog(@"Last month");
    } else {
        NSLog(@"A long time ago");
    }
于 2013-06-06T07:44:09.650 回答
2

在 Swift 中用“今天”和“昨天”字符串替换日期对象。

如果您想用不同的甲酸盐显示日期,请根据需要更改timeStyleand dateStyle

var df = DateFormatter()
df.timeStyle = .medium
df.dateStyle = .short
df.doesRelativeDateFormatting = true
// this enables relative dates like yesterday, today, tomorrow...
print("\(df.string(from: Date(timeIntervalSinceNow: -48 * 60 * 60)))")
print("\(df.string(from: Date(timeIntervalSinceNow: -24 * 60 * 60)))")
print("\(df.string(from: Date()))")
print("\(df.string(from: Date(timeIntervalSinceNow: 24 * 60 * 60)))")
print("\(df.string(from: Date(timeIntervalSinceNow: 48 * 60 * 60)))")

结果:

6/4/13, 9:13:22 AM 
Yesterday, 9:13:22 AM 
Today, 9:13:22 AM
Tomorrow, 9:13:22 AM 
6/8/13, 9:13:22 AM
于 2017-04-05T05:46:12.463 回答
0
    NSDate *todayDate = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *today = [dateFormat stringFromDate:todayDate];
    NSLog(@"today is %@",today);
    NSDate *yesterdayDate = [todayDate dateByAddingTimeInterval: -86400.0];
    NSString *yesterday = [dateFormat stringFromDate:yesterdayDate];
    NSLog(@"yesterday was %@",yesterday);

    NSString *yourDate = [NSString stringWithFormat:@"2013-06-08 12:33:28"];

    if ([yourDate isEqualToString:yesterday]) {

        NSLog(@"yesterday");
    }
    else if ([yourDate isEqualToString:today])
    {
        NSLog(@"today");

    }
    else
    {
        NSLog(@"the date is %@",yourDate);
    }

1)取出今天和昨天的日期,然后比较您输入的日期并相应打印

于 2013-06-06T07:00:34.793 回答
0
NSTimeInterval interval = [dict[@"deviceTimeStamp"]doubleValue]; // set your intervals
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval]; // set your past date or create it using dateWithIntervalSince1970 method
[formatter setDateFormat:@"hh:mm a"];
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: date];
NSDate *dateee =  [NSDate dateWithTimeInterval: seconds sinceDate: date];
NSDateComponents *components = [[NSCalendar currentCalendar] components:units fromDate:dateee toDate:[NSDate date] options:0];
    if (components.day > 0)
    {
        if (components.day > 1){
            [formatter setDateFormat:@"MMM dd"];
            NSString *dateString = [formatter stringFromDate:date];
            NSlog(@"%@",dateString);
        }
        else{
                NSlog(@"Yesterday");
        }
   }
   else{
                NSlog(@"Today");
   }
于 2016-11-23T11:58:08.607 回答