Fortunately, Calendar will bring all of us towards evaluate ( _ : in order to : toGranularity ) strategy, hence measures up a few schedules within certain quantity of accuracy.
- 5 minutes pursuing the minute whenever Fruit Silicone try launched
- 3 instances following the minute when Fruit Silicone try established
assist appleSiliconIntroDayPlus5Minutes = gregorianCalendar.date( byAdding: .minute, value: 5, to: appleSiliconIntroDate )! let appleSiliconIntroDatePlus3Hours = gregorianCalendar.date( byAdding: .hour, value: 3, to: appleSiliconIntroDate )!
With the Go out s laid out, you might set evaluate ( _ : to : toGranularity ) to help you usepare appleSiliconIntroDate to appleSiliconIntroDatePlus1Second in the second level of granularity.
let test1 = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus1Second, toGranularity: .second ) == .orderedSame printing("• appleSiliconIntroDate == appleSiliconIntroDatePlus1Second (with second granularity): \(test1)") let test2 = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus1Second, toGranularity: .second ) == .orderedAscending print("• appleSiliconIntroDate < appleSiliconIntroDatePlus1Second>\(test2)"
)
New yields will tell you you to definitely during the . next quantity of granularity, appleSiliconIntroDate and you may appleSiliconIntroDatePlus1Second aren’t the same time and you to definitely appleSiliconIntroDatePlus1Second takes place immediately after appleSiliconIntroDate .
let test3 = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus1Second, toGranularity: .minute ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus1Second (with minute granularity): \(test3)")
In the . second number of granularity, examine ( _ : so you can : toGranularity ) accounts one to appleSiliconIntroDate and you will appleSiliconIntroDatePlus1Second try roughly once. You will observe the same effects when you compare appleSiliconIntroDate and you can appleSiliconIntroDatePlus5Minutes during the the fresh . time quantity of granularity.
let test4 = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus5Minutes, toGranularity: .hour ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus5Minutes (with hour granularity): \(test4)")
let test5 = gregorianCalendar.compare( appleSiliconIntroDatePlus5Minutes, to: appleSiliconIntroDate, toGranularity: .minute ) == .orderedDescending print("• appleSiliconIntroDatePlus5Minutes > appleSiliconIntroDate (with minute granularity): \(test5)")
Modifying returning to the new . second amount of granularity, evaluate ( _ : in order to : toGranularity ) tend to report that appleSiliconIntroDatePlus5Minutes represents an occasion shortly after appleSiliconIntroDate .
let testsix = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus3Hours, toGranularity: .day ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus3Hours (with day granularity): \(test6)")
Figuring “Second Times”
” On a number of other platforms, this should wanted a lot of work, but in Quick, a call so you can Schedule ‘s the reason nextDate ( once : matching : matchingPolicy : repeatedTimePolicy : recommendations : ) system is all that’s necessary.
Applications one plan events will often have in order to estimate “2nd dates
The thought of “Second schedules” is a lot easier to exhibit unlike determine, so I’ll perform exactly that. Let’s start by particular password to show a night out together representing the newest the very next time it might be step three in the morning.
print("\nNext Times:") let next3AmComponents = DateComponents(hour: 3) let next3AmDate = gregorianCalendar.nextDate( after: Date(), matching: next3AmComponents, matchingPolicy: .nextTime )! let next3AmFormatted = dateFormatter.sequence(from: next3AmDate) print("• The next time it will be 3:00 a.m. is: \(next3AmFormatted).")
Which are the times of the earlier and next Vacations? nextDate ( ) will perform it formula in two contours off code.
let sundayComponents = DateComponents( weekday: 1 ) let previousSunday = gregorianCalendar.nextDate( after: Date(), matching: sundayComponents, matchingPolicy: .nextTime, direction: .backward prova webbplatsen )! let nextSunday = gregorianCalendar.nextDate( after: Date(), matching: sundayComponents, matchingPolicy: .nextTime, direction: .forward )! dateFormatter.timeStyle = .nothing let previousSundayFormatted = dateFormatter.string(from: previousSunday) let nextSundayFormatted = dateFormatter.string(from: nextSunday) print("• The previous Sunday was \(previousSundayFormatted).") print("• The next Sunday will be \(nextSundayFormatted).")
Of the mode the fresh new recommended advice : parameter to . backwards , you can buy brand new “early in the day second day.” That’s the method that you got the new day into the earlier in the day Week-end.
Of many conferences is arranged into the cousin terminology, instance “the next Friday of the times.” Whenever ‘s the next third Monday of one’s week?
let nextThirdFridayComponents = DateComponents( weekday: 6, weekdayOrdinal: 3 ) let nextThirdFridayDate = gregorianCalendar.nextDate( after: Date(), matching: nextThirdFridayComponents, matchingPolicy: .nextTime )! let nextThirdFridayFormatted = dateFormatter.string(from: nextThirdFridayDate) print("• The next third Friday of the month will be \(nextThirdFridayFormatted).")