使用 NSDate 并与 Objective C 中的日期进行比较

发布于 2024-10-10 13:02:57 字数 263 浏览 0 评论 0原文

"31-Dec-2010 9:00AM to 1:00PM"

以上面的 NSString 为例,我需要将其转换为 2 个 NSDate,例如 2010 年 12 月 31 日上午 9:00 和 2010 年 12 月 31 日下午 1:00

然后将其与当前日期进行比较,看看当前日期是否在给定日期内。

因此,2010 年 12 月 31 日上午 10:00 就属于其中。

我想知道 Objective C 优雅地做到这一点的最佳实践/技巧是什么?

"31-Dec-2010 9:00AM to 1:00PM"

Take the above NSString for example, I need to convert it to 2 NSDates e.g.
31-Dec-2010 9:00AM AND 31-Dec-2010 1:00PM

Then compare it with the current Date to see if the current date falls within the given dates.

So 31-Dec-2010 10:00AM would fall within.

I'm wondering What are the best practices/tricks are with Objective C to do this elegantly?

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

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

发布评论

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

评论(2

绝情姑娘 2024-10-17 13:02:57

我最终这样做了:

    NSString *temp = [[dateString allValues] objectAtIndex:0];

    NSLog(@"temp: %@", temp);


    NSArray *tokens = [temp componentsSeparatedByString: @" "];


    NSArray *tokenOneDelimited =  [[tokens objectAtIndex:0] componentsSeparatedByString: @"-"];


    NSString *dateStr1 = [NSString stringWithFormat: @"%@-%@-%@ %@",    [tokenOneDelimited  objectAtIndex:2],
                                                                        [tokenOneDelimited  objectAtIndex:1],
                                                                        [tokenOneDelimited  objectAtIndex:0],
                                                                        [tokens             objectAtIndex:1]];

    NSString *dateStr2 = [NSString stringWithFormat: @"%@-%@-%@ %@",    [tokenOneDelimited  objectAtIndex:2],
                          [tokenOneDelimited    objectAtIndex:1],
                          [tokenOneDelimited    objectAtIndex:0],
                          [tokens               objectAtIndex:3]];

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

    [dateFormatter setDateFormat:@"yyyy-MMM-dd hh:mma"];

    NSDate *myDate1 = [dateFormatter dateFromString: dateStr1];

    NSDate *myDate2 = [dateFormatter dateFromString: dateStr2];

    NSDate *currentDate = [NSDate date];

    NSComparisonResult comparison = [currentDate compare: myDate1];

    NSComparisonResult comparison2 = [currentDate compare: myDate2];


    if ( 
        comparison      == NSOrderedDescending &&
        comparison2     == NSOrderedAscending
        )
    {
        NSLog(@"is On now");

I ended up doing it like so:

    NSString *temp = [[dateString allValues] objectAtIndex:0];

    NSLog(@"temp: %@", temp);


    NSArray *tokens = [temp componentsSeparatedByString: @" "];


    NSArray *tokenOneDelimited =  [[tokens objectAtIndex:0] componentsSeparatedByString: @"-"];


    NSString *dateStr1 = [NSString stringWithFormat: @"%@-%@-%@ %@",    [tokenOneDelimited  objectAtIndex:2],
                                                                        [tokenOneDelimited  objectAtIndex:1],
                                                                        [tokenOneDelimited  objectAtIndex:0],
                                                                        [tokens             objectAtIndex:1]];

    NSString *dateStr2 = [NSString stringWithFormat: @"%@-%@-%@ %@",    [tokenOneDelimited  objectAtIndex:2],
                          [tokenOneDelimited    objectAtIndex:1],
                          [tokenOneDelimited    objectAtIndex:0],
                          [tokens               objectAtIndex:3]];

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

    [dateFormatter setDateFormat:@"yyyy-MMM-dd hh:mma"];

    NSDate *myDate1 = [dateFormatter dateFromString: dateStr1];

    NSDate *myDate2 = [dateFormatter dateFromString: dateStr2];

    NSDate *currentDate = [NSDate date];

    NSComparisonResult comparison = [currentDate compare: myDate1];

    NSComparisonResult comparison2 = [currentDate compare: myDate2];


    if ( 
        comparison      == NSOrderedDescending &&
        comparison2     == NSOrderedAscending
        )
    {
        NSLog(@"is On now");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文