检查指定日期是今天、昨天还是将来的日期

发布于 2024-12-09 03:41:56 字数 99 浏览 3 评论 0原文

我有一个关于 NSDate 的疑问。我有一个日期,即“2011-10-04 07:36:38 +0000”,我想检查这个日期是昨天、今天还是未来的日期。

我该怎么办呢?

I have one query regarding NSDate. I have a date i.e. "2011-10-04 07:36:38 +0000", and I want to check if this date is yesterday, or today or a future date.

How would I go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风情万种。 2024-12-16 03:41:56

试试这个:

注意:根据您的需要更改日期格式。

NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate* enteredDate = [df dateFromString:@"10/04/2011"];
NSDate * today = [NSDate date];
NSComparisonResult result = [today compare:enteredDate];
switch (result)
{
    case NSOrderedAscending: 
        NSLog(@"Future Date");
                    break;
    case NSOrderedDescending: 
        NSLog(@"Earlier Date");
                    break;
    case NSOrderedSame: 
        NSLog(@"Today/Null Date Passed"); //Not sure why This is case when null/wrong date is passed
                    break;
}

Try this:

Note: Change the date format as per your need.

NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate* enteredDate = [df dateFromString:@"10/04/2011"];
NSDate * today = [NSDate date];
NSComparisonResult result = [today compare:enteredDate];
switch (result)
{
    case NSOrderedAscending: 
        NSLog(@"Future Date");
                    break;
    case NSOrderedDescending: 
        NSLog(@"Earlier Date");
                    break;
    case NSOrderedSame: 
        NSLog(@"Today/Null Date Passed"); //Not sure why This is case when null/wrong date is passed
                    break;
}
拥有 2024-12-16 03:41:56

请参阅 Apple 关于日期计算的文档

NSDate *startDate = ...;
NSDate *endDate = ...;

NSCalendar *gregorian = [[NSCalendar alloc]
                 initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
                                          fromDate:startDate
                                          toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];

如果 days 介于 +1 和-1 那么你的约会对象就是“今天”的候选者。显然,您需要考虑如何处理时间。想必最简单的方法是将所有日期设置为相关日期的 00:00.00 小时(截断使用这样的方法计算日期),然后使用这些值进行计算。这样你今天会得到 0,昨天会得到 -1,明天会得到 +1,任何其他值同样会告诉你事情在未来或过去有多远。

See Apple's documentation on date calculations:

NSDate *startDate = ...;
NSDate *endDate = ...;

NSCalendar *gregorian = [[NSCalendar alloc]
                 initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
                                          fromDate:startDate
                                          toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];

If days is between +1 and -1 then your date is a candidate for being "today". Obviously you'll need to think about how you handle hours. Presumably the easiest thing would be to set all dates to be 00:00.00 hours on the day in question (truncate the date using an approach like this), and then use those values for the calculation. That way you'd get 0 for today, -1 for yesterday, +1 for tomorrow, and any other value would likewise tell you how far things were in the future or the past.

阿楠 2024-12-16 03:41:56

根据您的需要使用以下任何内容,

– earlierDate:
– laterDate:
– compare:

请参阅此 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

Use any of the folowing according to ur need,

– earlierDate:
– laterDate:
– compare:

Refer this http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

梦忆晨望 2024-12-16 03:41:56
-(NSString*)timeAgoFor:(NSString*)tipping_date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:tipping_date];
    NSString *key = @"";
    NSTimeInterval ti = [date timeIntervalSinceDate:[NSDate date]];
    key = (ti > 0) ? @"Left" : @"Ago";

    ti = ABS(ti);
    NSDate * today = [NSDate date];
    NSComparisonResult result = [today compare:date];

    if (result == NSOrderedSame) {
        return[NSString stringWithFormat:@"Today"];
    }
    else if (ti < 86400 * 2) {
        return[NSString stringWithFormat:@"1 Day %@",key];
    }else if (ti < 86400 * 7) {
        int diff = round(ti / 60 / 60 / 24);
        return[NSString stringWithFormat:@"%d Days %@", diff,key];
    }else {
        int diff = round(ti / (86400 * 7));
        return[NSString stringWithFormat:@"%d Wks %@", diff,key];
    }
}
-(NSString*)timeAgoFor:(NSString*)tipping_date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:tipping_date];
    NSString *key = @"";
    NSTimeInterval ti = [date timeIntervalSinceDate:[NSDate date]];
    key = (ti > 0) ? @"Left" : @"Ago";

    ti = ABS(ti);
    NSDate * today = [NSDate date];
    NSComparisonResult result = [today compare:date];

    if (result == NSOrderedSame) {
        return[NSString stringWithFormat:@"Today"];
    }
    else if (ti < 86400 * 2) {
        return[NSString stringWithFormat:@"1 Day %@",key];
    }else if (ti < 86400 * 7) {
        int diff = round(ti / 60 / 60 / 24);
        return[NSString stringWithFormat:@"%d Days %@", diff,key];
    }else {
        int diff = round(ti / (86400 * 7));
        return[NSString stringWithFormat:@"%d Wks %@", diff,key];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文