NSDate -dateWithNaturalLanguageString:在 iPhone 上?

发布于 2024-08-02 23:49:19 字数 582 浏览 4 评论 0原文

在 i电话文档集中NSDate,在讨论区他们讨论了 -dateWithNaturalLanguageString:locale:,但是他们没有在页面的其他地方记录该方法。

我之前在 iPhone 上使用过这个方法,虽然我收到了警告,但效果很好。现在我正在使用 -Werror 进行构建(我应该一直这样做^_^),我遇到了一个警告。

我将如何替换以下代码行?

NSDate *today = [NSDate dateWithNaturalLanguageString:@"today at 23:59:59"];
NSDate *tomorrow = [NSDate dateWithNaturalLanguageString:@"tomorrow at 23:59:59"];

In the iPhone docset for NSDate, in the discussion area they discuss -dateWithNaturalLanguageString:locale:, however they don't document the method elsewhere on the page.

I've used the method before for iPhone and it worked just fine, although I got warnings. Now that I'm building with -Werror (which I should have been doing all along ^_^) I've run into a warning with this.

How would I replace the following lines of code?

NSDate *today = [NSDate dateWithNaturalLanguageString:@"today at 23:59:59"];
NSDate *tomorrow = [NSDate dateWithNaturalLanguageString:@"tomorrow at 23:59:59"];

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

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

发布评论

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

评论(2

¢好甜 2024-08-09 23:49:19

一种方法是使用 NSCalendar 和 NSDateComponents。

要获取今天的 23:59:59:

NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit 
                                      fromDate:date];

[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];

NSDate *today = [calendar dateFromComponents:comps];

要获取明天的 23:59:59:

NSDate *now = [NSDate dateWithTimeIntervalSinceNow:24 * 60 * 60]; // 24h from now
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit 
                                      fromDate:date];

[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];

NSDate *tomorrow = [calendar dateFromComponents:comps];

One way to do this is with NSCalendar and NSDateComponents.

To get today at 23:59:59:

NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit 
                                      fromDate:date];

[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];

NSDate *today = [calendar dateFromComponents:comps];

To get tomorrow at 23:59:59:

NSDate *now = [NSDate dateWithTimeIntervalSinceNow:24 * 60 * 60]; // 24h from now
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit 
                                      fromDate:date];

[comps setHour:23];
[comps setMinute:59];
[comps setSecond:59];

NSDate *tomorrow = [calendar dateFromComponents:comps];
葵雨 2024-08-09 23:49:19

假设:

static const unsigned long seconds_per_day = 60 * 60 * 24;

您可以通过以下方式获取现在和现在+24 小时:

NSDate* today = [NSDate date]; // set for today
NSDate* tomorrow = [today addTimeInterval:seconds_per_day];

为了获取给定日期的午夜,请务必记住 NSDate* 是免费桥接到 CFDateRef,它提供了一些您想要使用的附加 API。特别是,您可以使用 CFDateGetAbsoluteTimeNSDate 转换为 CFAbsoluteTime

CFAbsoluteTime abstime = CFDateGetAbsoluteTime(reinterpret_cast<CFDateRef>(myNSDate));
long long      inttime = static_cast<long long>(abstime);

inttime = (inttime / seconds_per_day) * seconds_per_day; // clips the time to midnight

NSDate* myNSDateAtMidnight = reinterpret_cast<NSDate*>(CFDateCreate(NULL,
                                                       static_cast<CFAbsoluteTime>(inttime)));

...您可以将其包装到一个函数中以舍入两个 >今天明天到午夜。

Let's assume:

static const unsigned long seconds_per_day = 60 * 60 * 24;

You can get now and now+24 hours with:

NSDate* today = [NSDate date]; // set for today
NSDate* tomorrow = [today addTimeInterval:seconds_per_day];

In order to get midnight for the given day, it's important to remember that NSDate* is toll-free bridged to CFDateRef, which provides some additional APIs you'll want to use. In particular, you can convert an NSDate to a CFAbsoluteTime with CFDateGetAbsoluteTime:

CFAbsoluteTime abstime = CFDateGetAbsoluteTime(reinterpret_cast<CFDateRef>(myNSDate));
long long      inttime = static_cast<long long>(abstime);

inttime = (inttime / seconds_per_day) * seconds_per_day; // clips the time to midnight

NSDate* myNSDateAtMidnight = reinterpret_cast<NSDate*>(CFDateCreate(NULL,
                                                       static_cast<CFAbsoluteTime>(inttime)));

... which you can wrap into a function to round both today and tomorrow to midnight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文