So it reliability shall be times, era, months, plus

So it reliability shall be times, era, months, plus

Thankfully, Diary will bring all of us on examine ( _ : so you can : toGranularity ) approach, and this measures up several schedules at the a given number of accuracy.

  • 5 minutes following the moment when Fruit Silicone polymer is revealed
  • 3 circumstances following moment when Apple Silicone LatamDate dating is actually launched
help appleSiliconIntroTimePlus5Minutes = byAdding: .minute, value: 5, to: appleSiliconIntroDate )! let appleSiliconIntroDatePlus3Hours = byAdding: .hour, value: 3, to: appleSiliconIntroDate )!

With this Big date s discussed, you could put compare ( _ : to help you : toGranularity ) to help you usepare appleSiliconIntroDate in order to appleSiliconIntroDatePlusstep oneSecond within 2nd amount of granularity.

let test1 = appleSiliconIntroDate, to: appleSiliconIntroDatePlus1Second, toGranularity: .second ) == .orderedSame printing("• appleSiliconIntroDate == appleSiliconIntroDatePlus1Second (with second granularity): \(test1)") let test2 = appleSiliconIntroDate, to: appleSiliconIntroDatePlus1Second, toGranularity: .second ) == .orderedAscending print("• appleSiliconIntroDate < appleSiliconIntroDatePlus1Second>\(test2)")

The returns will tell you one to on . next quantity of granularity, appleSiliconIntroDate and appleSiliconIntroDatePlus1Second won’t be the same time and you to definitely appleSiliconIntroDatePlus1Second occurs after appleSiliconIntroDate .

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

On . 2nd number of granularity, contrast ( _ : so you’re able to : toGranularity ) accounts that appleSiliconIntroDate and you will appleSiliconIntroDatePlus1Second is more or less the same time. You will observe an identical influence when comparing appleSiliconIntroDate and you will appleSiliconIntroDatePlus5Minutes at the this new . hour level of granularity.

let test4 = appleSiliconIntroDate, to: appleSiliconIntroDatePlus5Minutes, toGranularity: .hour ) == .orderedSame print("• appleSiliconIntroDate == appleSiliconIntroDatePlus5Minutes (with hour granularity): \(test4)")
let test5 = appleSiliconIntroDatePlus5Minutes, to: appleSiliconIntroDate, toGranularity: .minute ) == .orderedDescending print("• appleSiliconIntroDatePlus5Minutes > appleSiliconIntroDate (with minute granularity): \(test5)")

Changing back into the fresh . time quantity of granularity, evaluate ( _ : so you’re able to : toGranularity ) often declare that appleSiliconIntroDatePlus5Minutes means an occasion immediately following appleSiliconIntroDate .

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

Figuring “2nd Dates”

” For the a great many other networks, this should need many really works, but in Swift, a call to help you Schedule is why nextDate ( just after : coordinating : matchingPolicy : repeatedTimePolicy : assistance : ) system is all that’s necessary.

Software one agenda situations will often have to help you calculate “next schedules

The idea of “Second dates” is a lot easier to display in lieu of describe, therefore I will carry out exactly that. Let us start by particular password to exhibit a date symbolizing the fresh next time it would be 3 are.

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

Do you know the schedules of your prior and next Vacations? nextDate ( ) will do it formula in two contours from 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 = .none 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 means the new elective advice : parameter so you’re able to . backward , you can purchase new “previous 2nd go out.” Which is the way you got new date into the early in the day Weekend.

Of many group meetings was arranged inside the relative terms, eg “the third Tuesday of your month.” Whenever is the 2nd third Saturday of your own month?

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

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *