Easiest way to get an NSDate representation of “the next 7am that will occur” in Cocoa?

后端 未结 1 1606
花落未央
花落未央 2021-01-20 07:20

I figure I can achieve what I want by using NSCalendar and NSDateComponents, but that would run something like the following:

  • Get \"n
相关标签:
1条回答
  • 2021-01-20 07:38

    I haven't tried it, but I think this would work:

    • Get Now date.
    • Create NSDateComponents from Now.
    • Set hour component to 7.
    • Convert 7am date components back to an NSDate.
    • If the 7am date is later than Now, you're done.
    • Otherwise, use -[NSCalendar dateByAddingComponents:options:] to advance the 7am date by one day.

    I'm not sure if that last step would do the right thing if a daylight savings change happened between one 7am and the next.

    0 讨论(0)
提交回复
热议问题