So it reliability is going to be minutes, period, months, and more

So it reliability is going to be minutes, period, months, and more

Thank goodness, Calendar provides you into the examine ( _ : to : toGranularity ) approach, and this measures up several times from the certain amount of accuracy.

  • 5 minutes following second when Fruit Silicon are announced
  • step three circumstances pursuing the time when Fruit Silicone polymer are established
assist appleSiliconIntroTimePlus5Minutes = gregorianCalendar.date( byAdding: .minute, value: 5, to: appleSiliconIntroDate )! let appleSiliconIntroDatePlus3Hours = gregorianCalendar.date( byAdding: .hour, value: 3, to: appleSiliconIntroDate )!

With the Time s discussed, you might place examine ( _ : in order to : toGranularity ) so you’re able to usepare appleSiliconIntroDate to help you appleSiliconIntroDatePlus1Second from the next amount 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 https://internationalwomen.net/sv/ryska-flickor/ print("• appleSiliconIntroDate \(test2)")

This new production will tell you you to at . second quantity of granularity, appleSiliconIntroDate and you will appleSiliconIntroDatePlus1Second won’t be the same some time that appleSiliconIntroDatePlus1Second happen immediately after appleSiliconIntroDate .

let test3 = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus1Second, toGranularity: .minute ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus1Second (with minute granularity): \(test3)")

Within . 2nd amount of granularity, compare ( _ : so you can : toGranularity ) records you to definitely appleSiliconIntroDate and you will appleSiliconIntroDatePlus1Second try more or less the same time frame. You will see a similar effect when you compare appleSiliconIntroDate and you can appleSiliconIntroDatePlus5Minutes from the new . time 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)")

Altering returning to the fresh . moment number of granularity, compare ( _ : in order to : toGranularity ) usually claim that appleSiliconIntroDatePlus5Minutes signifies a time after appleSiliconIntroDate .

let testsix = gregorianCalendar.compare( appleSiliconIntroDate, to: appleSiliconIntroDatePlus3Hours, toGranularity: .day ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus3Hours (with day granularity): \(test6)")

Calculating “Next Times”

” On the a great many other systems, this will require a number of works, in Quick, a visit to help you Calendar is why nextDate ( immediately after : matching : matchingPolicy : repeatedTimePolicy : guidelines : ) system is all that’s necessary.

Software you to definitely agenda incidents will often have in order to determine “next times

The idea of “Next times” is easier to exhibit rather than explain, thus I am going to create just that. Why don’t we start by particular code to exhibit a night out together symbolizing the fresh new next time it could be 3 am.

print("\nNext Schedules:") 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 your previous and next Vacations? nextDate ( ) will perform this computation in two traces out of 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 = .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 the function the fresh recommended advice : factor to . backward , you can aquire this new “past second date.” That’s the way you had brand new big date with the previous Week-end.

Of several conferences are planned into the cousin terms, eg “the next Monday of the times.” Whenever ‘s the second 3rd Friday of the few days?

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).")

Добавить комментарий