Which precision is moments, occasions, weeks, and much more

The good news is, Calendar brings united states to the examine ( _ : so you’re able to : toGranularity ) method, hence measures up a few schedules at certain amount of precision.

  • five full minutes adopting the time when Apple Silicon is actually launched
  • step three occasions after the moment when Fruit Silicon try announced
assist appleSiliconIntroGo outPlus5Minutes = gregorianCalendar.date( byAdding: .minute, value: 5, to: appleSiliconIntroDate )! let appleSiliconIntroDatePlus3Hours = gregorianCalendar.date( byAdding: .hour, value: 3, to: appleSiliconIntroDate )!

With these Time s defined, you could set examine ( _ : in order to : toGranularity ) to usepare appleSiliconIntroDate to help you appleSiliconIntroDatePlus1Second in 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 print("• appleSiliconIntroDate < appleSiliconIntroDatePlus1Second>\(test2)")

Brand new returns will tell you you to definitely at the . next number of granularity, appleSiliconIntroDate and appleSiliconIntroDatePlus1Second are not the same some time and that appleSiliconIntroDatePlus1Second happen immediately following appleSiliconIntroDate .

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

From the . second quantity of granularity, examine ( _ : so you’re able to : toGranularity ) profile that appleSiliconIntroDate and you will appleSiliconIntroDatePlus1Second was about once. You’ll see an identical influence when you compare appleSiliconIntroDate and you may appleSiliconIntroDatePlus5Minutes at the . hr 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 back to this new . minute amount of granularity, examine ( _ : in order to : toGranularity ) tend to report that appleSiliconIntroDatePlus5Minutes means a time immediately after appleSiliconIntroDate .

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

Figuring “2nd Schedules”

” Into a great many other platforms, this should need lots of works, in Quick, a visit so you’re able to Schedule is why nextDate ( immediately after : coordinating : matchingPolicy : repeatedTimePolicy : advice : ) method is all you want.

Applications you to definitely schedule occurrences normally have so you can calculate “next times

The idea of “2nd times” is a lot easier to show as opposed to identify, thus I shall carry out that. Why don’t we start by some password showing a romantic date representing the newest the next time it will be 3 was.

print("\nNext Schedules:") let next3AmComponents = DateComponents(hour: 3) let next3AmDate = gregorianCalendar.nextDate( after: Date(), matching: next3AmComponents, matchingPolicy: .nextTime )! let next3AmFormatted = dateFormatter.string(from: next3AmDate) print("• The next time it will be 3:00 a.m. is: \(next3AmFormatted).")

Exactly what are the dates of one’s previous and next Weekends? nextDate ( ) perform it formula in 2 lines from 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 = .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).")

From the means the newest recommended guidelines : parameter so you’re able to . backwards , you can purchase the latest “early in the day next date.” Which is the method that you got this new big date with the early in the day Sunday.

Many conferences are arranged for the cousin conditions, instance “the 3rd Monday of the day.” When is the 2nd third Friday of the day?

let nextThirdFridayComponents = DateComponents( weekday: 6, weekdayOrdinal: 3 ) let nextThirdFridayDate = gregorianCalendar.nextDate( after: Date(), matching: nextThirdFridayComponents, matchingPolicy: kissbrides.com titta över till dessa killar .nextTime )! let nextThirdFridayFormatted = dateFormatter.string(from: nextThirdFridayDate) print("• The next third Friday of the month will be \(nextThirdFridayFormatted).")

TAGS

Comments are closed