我如何比较“今天”和“今天”? (NSDate) 到格式化字符串日期 (dd/MM/yyyy)?

发布于 2024-10-19 08:02:51 字数 898 浏览 2 评论 0原文

我是 iOS 编程新手,一直在寻找一种将“今天”(NSDate)与格式化字符串日期(dd/MM/yyyy)进行比较的方法。

我在 NSDate 的 earlierDatelaterDate 等方面找到了一些很好的答案,但代码似乎在我的项目中不起作用。

就是这样:

// [book quandd]  is the string :    25/02/2011  (or whatever dd/MM/yyyy date)

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

if ( [[dateFormat dateFromString:[book quandd]] isEqualToDate:today]) {
    NSLog(@"Dates are equal");
}
if ( [[dateFormat dateFromString:[book quandd]] earlierDate:today]) {
    NSLog(@"earlier");
}
if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]) {
    NSLog(@"later");
}

无论我做什么,无论 [book quandd] 中的日期是什么,控制台总是写“早”和“晚”,就好像它们同时比 今天

这个“bug”从哪里来?

例如,是否有一些简单/更简单的方法可以将今天日期与2011年2月26日进行比较?

I'm new to iOS programming and I've been looking a lot for a way to compare "today" (an NSDate) to a formatted string date (dd/MM/yyyy).

I've found some good answers with the NSDate's earlierDate, laterDate etc. but the code doesn't seem to work in my project.

here it is :

// [book quandd]  is the string :    25/02/2011  (or whatever dd/MM/yyyy date)

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

if ( [[dateFormat dateFromString:[book quandd]] isEqualToDate:today]) {
    NSLog(@"Dates are equal");
}
if ( [[dateFormat dateFromString:[book quandd]] earlierDate:today]) {
    NSLog(@"earlier");
}
if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]) {
    NSLog(@"later");
}

Whatever I do and whatever the date in [book quandd], the console always writes "earlier" and "later", as if they were later and earlier at the same time than today.

Where is that "bug" coming from ?

Is there some easy/easier way to compare the today date with 26/02/2011 for example ?

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

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

发布评论

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

评论(5

凉世弥音 2024-10-26 08:02:51

NSDate 包含日期和时间。除非时间精确到毫秒(如果不是更精细),否则两个 NSDate 值不会相等。当你执行[NSDate date]时,你会得到“现在”的确切时间,而如果你使用 NSDateFormatter 将字符串转换为日期(没有时间值),则使用午夜。

如果你想查看两个 NSDate 是否在同一天,你可以使用 NSCalendar & NSDateComponent。或者您可以“作弊”并将两个日期格式化为没有时间值的日期字符串,然后比较这些字符串。

- (void) CompareDate:(NSDate*)date1 toDate:(NSDate*)date2 {
    NSDateFormatter* fmt = [NSDateFormatter new];
    [fmt setDateFormat:@"yyyyMMdd"];
    // Note that you should set the appropriate timezone if you don't want the default.
    NSString* date1Str = [fmt stringFromDate:date1];
    NSString* date2Str = [fmt stringFromDate:date2];
    switch ([date1Str compare:date2Str]) {
        case NSOrderedAscending:
            NSLog(@"Date 1 is earlier");
            break;
        case NSOrderedSame:
            NSLog(@"Dates are equal");
            break;
        case NSOrderedDescending:
            NSLog(@"Date 2 is earlier");
            break;
    }
}

An NSDate includes both date and time. No two NSDate values will be equal unless the times are identical down to the millisecond (if not finer). When you do [NSDate date] you get the exact time of "now", while if you use NSDateFormatter to convert a string to date (with no time value) then midnight is used.

If you want to see if two NSDates fall on the same day you can use NSCalendar & NSDateComponents. Or you can "cheat" and format both dates to a date string with no time value and compare the strings.

- (void) CompareDate:(NSDate*)date1 toDate:(NSDate*)date2 {
    NSDateFormatter* fmt = [NSDateFormatter new];
    [fmt setDateFormat:@"yyyyMMdd"];
    // Note that you should set the appropriate timezone if you don't want the default.
    NSString* date1Str = [fmt stringFromDate:date1];
    NSString* date2Str = [fmt stringFromDate:date2];
    switch ([date1Str compare:date2Str]) {
        case NSOrderedAscending:
            NSLog(@"Date 1 is earlier");
            break;
        case NSOrderedSame:
            NSLog(@"Dates are equal");
            break;
        case NSOrderedDescending:
            NSLog(@"Date 2 is earlier");
            break;
    }
}
拧巴小姐 2024-10-26 08:02:51

使用 timeIntervalSinceNow: 获取与必要日期的差异。

您还应该尝试获取它们之间的差异,然后转换为字符串。

Use timeIntervalSinceNow: to get the diff from the necessary date.

You should also try to get the difference between them and then convert to a string.

小忆控 2024-10-26 08:02:51

用这个

NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];

    if ( [[dateFormat dateFromString:[book quandd]] compare:today]==NSOrderedSame) {
        NSLog(@"Dates are equal");
    }
    if ( [[dateFormat dateFromString:[book quandd]] compare:today]==NSOrderedAscending) {
        NSLog(@"earlier");
    }
    if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]==NSOrderedDescending) {
        NSLog(@"later");
    }

use this

NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];

    if ( [[dateFormat dateFromString:[book quandd]] compare:today]==NSOrderedSame) {
        NSLog(@"Dates are equal");
    }
    if ( [[dateFormat dateFromString:[book quandd]] compare:today]==NSOrderedAscending) {
        NSLog(@"earlier");
    }
    if ( [[dateFormat dateFromString:[book quandd]] laterDate:today]==NSOrderedDescending) {
        NSLog(@"later");
    }
毁我热情 2024-10-26 08:02:51

您需要使用引用的日期之一检查 previousDate 的返回值。例如: if([date1 previousDate:date2] == date1)

You need to check return value of the earlierDate with one of the dates referred. Ex: if([date1 earlierDate:date2] == date1)

慵挽 2024-10-26 08:02:51

您的“错误”发生是因为laterDate和earlyDate返回早于或晚于的日期对象。返回对象始终为非零,因此两个 if 语句的计算结果均为 true。使用它们的正确方法是这样的:

NSString *book = @"19/08/2014";

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

NSDate *date = [dateFormat dateFromString:book];
if ( [date timeIntervalSinceDate:today] == 0) {
    NSLog(@"Dates are equal");
}
if ( [date laterDate:today] == today ) {
    NSLog(@"earlier");
}
if ( [date laterDate:today] == date) {
    NSLog(@"later");
}

或者,我使用了 timeIntervalSinceDate,它返回第一个日期和第二个日期之间的秒数。

NSString *book = @"19/08/2014";

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

NSDate *date = [dateFormat dateFromString:book];
if ( [date timeIntervalSinceDate:today] == 0) {
    NSLog(@"Dates are equal");
}
if ( [date timeIntervalSinceDate:today] < 0 ) {
    NSLog(@"earlier");
}
if ( [date timeIntervalSinceDate:today] > 0) {
    NSLog(@"later");
}

Your "bug" happens because laterDate and earlierDate return the date object that is either earlier or later. The return object is always nonnil, so both if statements evaluate to true. The correct way to use them is this way:

NSString *book = @"19/08/2014";

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

NSDate *date = [dateFormat dateFromString:book];
if ( [date timeIntervalSinceDate:today] == 0) {
    NSLog(@"Dates are equal");
}
if ( [date laterDate:today] == today ) {
    NSLog(@"earlier");
}
if ( [date laterDate:today] == date) {
    NSLog(@"later");
}

Alternatively, I've used timeIntervalSinceDate, which returns the seconds between the first date and the second date.

NSString *book = @"19/08/2014";

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];

NSDate *date = [dateFormat dateFromString:book];
if ( [date timeIntervalSinceDate:today] == 0) {
    NSLog(@"Dates are equal");
}
if ( [date timeIntervalSinceDate:today] < 0 ) {
    NSLog(@"earlier");
}
if ( [date timeIntervalSinceDate:today] > 0) {
    NSLog(@"later");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文