计算iOS中给定时间点之前的时间

发布于 2024-11-02 05:04:28 字数 1209 浏览 0 评论 0原文

我想知道现在与下周三 14:00 之间的秒差。

我发现 [NSDate date]; 为我提供了当前时间和日期,并且 NSTimeInterval 通常用于此目的(我想使用这个间隔在其他地方,而其他地方需要 NSTimeInterval),因此代码如下所示:

NSDate *now = [NSDate date];
NSDate *nextWednesday = ???;
// profit
NSTimeInterval secondsUntilNextWednesday = [nextWednesday timeIntervalSinceDate:now];

但是如何获取下周三的日期?

Edit2:我改进了这个方法,我仍然很不喜欢它,但这是一个巨大的改进。而且,这更让人想起我的伪尝试。

- (NSTimeInterval)secondsUntilNextWednesday
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDate *today = [NSDate date];
    NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: today];
    // 17 == 15:00, seemingly. (timezones)
    [components setHour:17];
    [components setWeekday:4];
    NSDate *wednesday = [calendar dateFromComponents:components];
    NSTimeInterval secondsToWednesday = [wednesday timeIntervalSinceDate:today];
    // if secondsToWednesday is negative, it has already been wednesday, 15:00 (or 16:00 depending on DST)
    if (secondsToWednesday < 0)
    {
        secondsToWednesday += 604800;
    }
    return secondsToWednesday;
}

I want to know the difference in seconds between now and, say, the next wednesday, 14:00.

I have found that [NSDate date]; gives me the current time and date, and that an NSTimeInterval would typically be used for this purpose (and I would like to use this interval somewhere else, and the somewhere else wants an NSTimeInterval), so the code would look like this:

NSDate *now = [NSDate date];
NSDate *nextWednesday = ???;
// profit
NSTimeInterval secondsUntilNextWednesday = [nextWednesday timeIntervalSinceDate:now];

But how do I get the date of next wednesday?

Edit2: I sort of improved the method, I still pretty much don't like it, but it's a huge improvement. Also, it's much more reminiscent of my pseudo-attempt.

- (NSTimeInterval)secondsUntilNextWednesday
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDate *today = [NSDate date];
    NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: today];
    // 17 == 15:00, seemingly. (timezones)
    [components setHour:17];
    [components setWeekday:4];
    NSDate *wednesday = [calendar dateFromComponents:components];
    NSTimeInterval secondsToWednesday = [wednesday timeIntervalSinceDate:today];
    // if secondsToWednesday is negative, it has already been wednesday, 15:00 (or 16:00 depending on DST)
    if (secondsToWednesday < 0)
    {
        secondsToWednesday += 604800;
    }
    return secondsToWednesday;
}

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

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

发布评论

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

评论(1

在你怀里撒娇 2024-11-09 05:04:28

Apple 提供了一个方便的指南,其中包含您需要了解的有关日期的所有信息: 日期和时间编程指南

您可能想查看一下 NSDateComponentsNSCalendar 类。

请参阅日历、日期组件和日历单位 >指南中的从组件创建日期中的示例基于不同的已知日期组件创建日期。

例如,该指南还展示了如何根据日期组件计算相对于当前日期的下一个星期三。

Apple provides a handy guide with all you need to know about dates: the Date and Time Programming Guide

You probably want to take a look at the classes NSDateComponents and NSCalendar.

See Calendars, Date Components, and Calendar Units > Creating a Date from Components in the guide for an example to create a date based on different known date components.

The guide also shows how to calculate based on date components to get to the next wednesday relative to the current date for example.

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