DateBuilder allows you to create Date and DateComponents instances with ease in a visual and declarative manner. With DateBuilder, it’s very trivial to define dates from as simple as “tomorrow at 9pm” or as complex as “first fridays for the next 24 months, at random times between 3pm and 7pm”.
Every DateBuilder expression ends on a specific day (or a set of days if you use functions like EveryDay/EveryMonth/etc.). First you specify your expression down to a day, and then define the time of day by calling at(hour:minute:) function. For example:
NextWeek()
.firstDay
.at(hour: 10, minute: 15)
Once you have your at expression, your date is now fully resolved. You can get a ready-to-use Date or DateComponents instance by calling .date() or .dateComponents().
So we start on the scale of years, then we notch it down to the scale of months, and then we finally get the specific day, which in this case will be the first thursday of a 4th month of the next year. After that, we finalize our query by using the at function.
NOTE: the start and end of the week is determined by the currently set Calendar and its Locale. To learn how to customize the calendar object used for DateBuilder queries, see “Customizing the Calendar / Locale / Timezone” section below
// top-level
ThisWeek()
NextWeek()
WeekOf(account.createdAt)
WeekOf(Today()) // use any `DateBuilder.Day` instance here
AddingWeeks(5, to: .thisWeek)
EveryWeek(forWeeks: 10, starting: .nextWeek)
// instance
ThisWeek()
--->.addingWeeks(10) // Week
--->.firstDay // Day
--->.lastDay // Day
--->.allDays // [Day]
--->.weekday(.thursday) // Day
--->.weekendStartDay // Day
--->.weekendEndDay // Day
Month
// top-level
ThisMonth()
NextMonth()
MonthOf(account.createdAt)
MonthOf(Today()) // use any `DateBuilder.Day` instance here
ExactMonth(year: 2021, month: 03)
AddingMonths(3, to: .thisMonth)
EveryMonth(forMonths: 5, starting: .monthOf(account.createdAt))
// instance
ThisMonth()
--->.addingMonths(5) // Month
--->.firstDay // Day
--->.lastDay // Day
--->.allDays // [Day]
--->.first(.saturday) // Day
--->.weekday(.third, .friday) // Day
Year
// top-level
ThisYear()
NextYear()
YearOf(account.createdAt)
YearOf(Tomorrow()) // use any `DateBuilder.Day` instance here
YearOf(NextMonth()) // use any `DateBuilder.Month` instance here
ExactYear(year: 2022)
AddingYears(1, to: ThisYear())
EveryYear(forYears: 100, starting: .thisYear)
// instance
ThisYear()
--->.addingYears(1) // Year
--->.firstMonth // Month
--->.lastMonth // Month
--->.allMonths // [Month]
ExactlyAt creates a resolved date from the existing Date instance. You can then use it to perform easy date calculations (functions addingMinutes/addingHours etc.) and easily get Date or DateComponents instances.
You can use EveryDay, EveryWeek, EveryMonth and EveryYear functions in the same way as you would use something like Today() or NextYear(). The only difference is that at the end you will get an array of dates instead of a single instance:
DateBuilder
DateBuilder allows you to create
DateandDateComponentsinstances with ease in a visual and declarative manner. With DateBuilder, it’s very trivial to define dates from as simple as “tomorrow at 9pm” or as complex as “first fridays for the next 24 months, at random times between 3pm and 7pm”.Maintainer: @dreymonde
As of now, DateBuilder is in beta. Some APIs might be changed between releases.
DateBuilder is a stand-alone part of NiceNotifications, a framework that radically simplifies local notifications, from content to permissions.
Usage
Guide
Anatomy of a date builder
Every DateBuilder expression ends on a specific day (or a set of days if you use functions like
EveryDay/EveryMonth/etc.). First you specify your expression down to a day, and then define the time of day by callingat(hour:minute:)function. For example:Once you have your
atexpression, your date is now fully resolved. You can get a ready-to-useDateorDateComponentsinstance by calling.date()or.dateComponents().Slightly more complicated example would be:
So we start on the scale of years, then we notch it down to the scale of months, and then we finally get the specific day, which in this case will be the first thursday of a 4th month of the next year. After that, we finalize our query by using the
atfunction.Available functions
Day
Week
NOTE: the start and end of the week is determined by the currently set
Calendarand itsLocale. To learn how to customize the calendar object used for DateBuilder queries, see “Customizing the Calendar / Locale / Timezone” section belowMonth
Year
Resolving the date
You can also get the
DateComponents(but notDate) instance by callingdateComponents()on an instance ofDateBuilder.Day, without usingat:Using
ExactlyAtfunctionExactlyAtcreates a resolved date from the existingDateinstance. You can then use it to perform easy date calculations (functionsaddingMinutes/addingHoursetc.) and easily getDateorDateComponentsinstances.Using
EveryfunctionsYou can use
EveryDay,EveryWeek,EveryMonthandEveryYearfunctions in the same way as you would use something likeToday()orNextYear(). The only difference is that at the end you will get an array of dates instead of a single instance:In case you use
.at(.randomTime( ... ))function withEveryfunctions, the exact resolved time will be different each day.Customizing the Calendar / Locale / Timezone
By default, DateBuilder uses
Calendar.currentfor all calculations. If you need to customize it, you can either change it globally:Or temporarily, using the
DateBuilder.withCalendarfunction:DateBuilder will return to its global
Calendarinstance after evaluating the expression.In a similar manner, you can also use
DateBuilder.withTimeZoneandDateBuilder.withLocalefunctions:All of these functions support returning the result of the closure (see above).
Installation
Swift Package Manager
http://github.com/nicephoton/DateBuilder.git.Acknowledgments
Special thanks to:
Related materials: