用iphone中的“今天”和“昨天”字符串替换日期对象

时间:2013-06-06 06:44:26

标签: iphone objective-c nsdate

我想用字符串“今天”和“昨天”返回我的日期对象,并在目标C中返回日期。欢迎所有评论:

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

6 个答案:

答案 0 :(得分:25)

NSDateFormatter可以做到这一点。但是,这不适用于自定义日期格式,但在大多数情况下,当您需要相对日期时,您首先将它们呈现给用户并you should not use hard coded date formats

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”(后天)的第一个和最后一个日期。

答案 1 :(得分:5)

NSDateFormatter s setDoesRelativeDateFormatting怎么样?

  

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

     

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

看看:NSDateFormatter class reference

答案 2 :(得分: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");
    }

答案 3 :(得分:2)

用Swift中的“today”和“yesterday”字符串替换日期对象。

如果您想使用不同的格式显示日期,请根据需要更改timeStyledateStyle

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

答案 4 :(得分: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)取出今天和昨天的日期,然后比较您输入的日期并相应地打印

答案 5 :(得分: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");
   }