The good news is, Calendar brings us to your contrast ( _ : in order to : toGranularity ) method, and that measures up several schedules within a given quantity of precision.
- 5 minutes following the moment when Fruit Silicon was launched
- 3 era following time when Apple Silicone was revealed
assist appleSiliconIntroDayPlus5Minutes = gregorianCalendar.date( byAdding: .minute, value: 5, to: appleSiliconIntroDate )! let appleSiliconIntroDatePlus3Hours = gregorianCalendar.date( byAdding: .hour, value: 3, to: appleSiliconIntroDate )!
With these Big date s discussed, you could potentially lay contrast ( _ : so you can : toGranularity ) so you’re able to usepare appleSiliconIntroDate so you can appleSiliconIntroDatePlusstep oneSecond on second 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)"
)
This new returns will tell you one in the . 2nd quantity of granularity, appleSiliconIntroDate and you can appleSiliconIntroDatePlus1Second are not the same some time one appleSiliconIntroDatePlus1Second happen shortly after appleSiliconIntroDate .
let test3 = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus1Second, toGranularity: .minute ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus1Second (with minute granularity): \(test3)")
On . next level of granularity, compare ( _ : so you can : toGranularity ) reports one appleSiliconIntroDate and you can appleSiliconIntroDatePlus1Second try roughly once. You will see a comparable influence when you compare appleSiliconIntroDate and appleSiliconIntroDatePlus5Minutes within the brand new . hr number 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)")
Changing back once again to the fresh . time amount of granularity, evaluate ( _ : in order to : toGranularity ) have a tendency to declare that appleSiliconIntroDatePlus5Minutes signifies a period of time immediately following appleSiliconIntroDate .
let test6 = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus3Hours, toGranularity: .day ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus3Hours (with day granularity): \(test6)")
Calculating “Second Dates”
” Into many other networks, this would need enough works, but in Quick, a trip in order to Diary is why nextDate ( just after : matching : matchingPolicy : repeatedTimePolicy : recommendations : ) system is all you need.
Programs you to schedule situations normally have so you’re able to assess “second times
The idea of “Next schedules” is easier to demonstrate rather than describe, so I’ll perform that. Let us start by specific password to display a night out together symbolizing new the very next time it might be 3 am.
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).")
What are the dates of your own previous and then Weekends? nextDate ( ) perform that it calculation in 2 lines regarding code.
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 vackra ööön kvinnor = .not one 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 means the newest recommended assistance : factor to . backward , you should buy the brand new “previous 2nd date.” That’s how you had brand new big date for the prior Week-end.
Of many meetings was booked inside the relative conditions, including “the third Friday of the day.” Whenever ‘s the next 3rd Tuesday of your own times?
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).")