Thankfully, Calendar provides all of us into contrast ( _ : so you’re able to : toGranularity ) strategy, hence measures up a couple schedules at the confirmed amount of precision.
- 5 minutes pursuing the time whenever Fruit Silicone polymer try launched
- step step 3 instances following the time when Fruit Silicon try revealed
help appleSiliconIntroGo outPlus5Minutes = gregorianCalendar.date( byAdding: .minute, value: 5, to: appleSiliconIntroDate )! let appleSiliconIntroDatePlus3Hours = gregorianCalendar.date( byAdding: .hour, value: 3, to: appleSiliconIntroDate )!
With these Go out s discussed, you can lay examine ( _ : to : toGranularity ) so you can usepare appleSiliconIntroDate in order to appleSiliconIntroDatePlusstep oneSecond during the 2nd quantity 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 efficiency will tell you you to at the . 2nd quantity of granularity, appleSiliconIntroDate and you may appleSiliconIntroDatePlus1Second won’t be the same some time you to definitely appleSiliconIntroDatePlus1Second happen after appleSiliconIntroDate .
let test3 = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus1Second, toGranularity: .minute ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus1Second (with minute granularity): \(test3)")
In the . next quantity of granularity, contrast ( _ : in order to : toGranularity ) accounts one appleSiliconIntroDate and you will appleSiliconIntroDatePlus1Second is more or less the same time. You will observe a comparable impact when comparing appleSiliconIntroDate and appleSiliconIntroDatePlus5Minutes at the the newest . time amount 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)")
Switching returning to new . second level of granularity, contrast ( _ : to help you : toGranularity ) will claim that appleSiliconIntroDatePlus5Minutes stands for a period of time after appleSiliconIntroDate .
let test6 = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus3Hours, toGranularity: .day ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus3Hours (with day granularity): \(test6)")
Calculating “Second Dates”
” On a great many other networks, this will need a number of performs, but in Quick, a trip to Calendar ‘s nextDate ( shortly after : coordinating : matchingPolicy : repeatedTimePolicy : assistance : ) method is all you want.
Applications one to agenda incidents often have so you can determine “next dates
The thought of “Next dates” is a lot easier to demonstrate rather than determine, thus I’ll manage just that. Why don’t we start by some password showing a romantic date representing the brand new next time it might be 3 was.
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).")
Do you know the times of your early in the day and next Sundays? nextDate ( ) perform so it formula in 2 outlines off password.
let sundayComponents = DateComponents( weekday: 1 ) let previousSunday = gregorianCalendar.nextDate( after: Date(), matching: sundayComponents, matchingPolicy: .nextTime, direction: .backward )! let nextSunday = gregorianCalendar.nextDate( after: Date(), matching: sundayComponents, matchingPolicy: .nextTime, direction: .forward )! dateFormatter.timeStyle = officiell webbplats.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).")
By the function new optional guidelines : parameter in order to . backwards , you can get this new “prior 2nd time.” That is the way you got the brand new go out toward past Sunday.
Of a lot meetings is actually arranged into the cousin conditions, such as for instance “the third Friday of your month.” Whenever ‘s the second third Tuesday of the 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).")
ความเห็นล่าสุด