6

If current date is 3 August 2018 (friday), i want to get 4 & 5 august 2018 (saturday and sunday)

if current date is 4 august (saturday), I still want to get 4 & 5 august 2018 (saturday and sunday)

if current date is 6 august (monday) then I want to get 11 & 12 August (saturday and sunday)

how to do that in swift?

sarah
  • 3,819
  • 4
  • 38
  • 80
  • What Timezone is today? perhaps that is your issue. If today is UTC it might actually be your tomorrow, which is Saturday and give you next Saturday August 11 – Brian Ogden Aug 04 '18 at 04:34
  • 2
    I just marked your question down, at least before you had code you attempted, you altered your answer to ask simply how to do it without any attempt of your own – Brian Ogden Aug 04 '18 at 05:08
  • 1
    Try this? https://stackoverflow.com/questions/38631176/how-can-i-find-the-next-weekend-swift – Dave Kasper Aug 04 '18 at 05:09
  • @BrianOgden I am sorry, but I don't wanna limit the answers from other just based on the code from the thread I shared before – sarah Aug 04 '18 at 05:38
  • @MartijnPeters Actually that's not a duplicate. – vadian Aug 04 '18 at 20:02

2 Answers2

7

Calendar has convenience methods for that

  • dateIntervalOfWeekend(containing:start:interval:) checks if the given date is in a weekend and returns the startDate and interval(duration in seconds) in the inout parameters. The Bool return value is true if the given date is within a weekend.

  • nextWeekend(startingAfter:start:interval:) returns startDate und interval in the inout parameters for the upcoming (.forward parameter) or passed (.backward) weekend.


let now = Date()
var startDate = Date()
var interval : TimeInterval = 0.0
if !Calendar.current.dateIntervalOfWeekend(containing: now, start: &startDate, interval: &interval) {
    Calendar.current.nextWeekend(startingAfter: now, start: &startDate, interval: &interval, direction: .forward)
}
print(startDate, startDate.addingTimeInterval(interval))

If you need start of Saturday and start of Sunday then replace the last line with

let endDate = startDate.addingTimeInterval(interval-1)
print(startDate, Calendar.current.startOfDay(for: endDate))

Alternatively – suggested by Martin R (thanks) – use dateIntervalOfWeekend(containing:) / nextWeekend(startingAfter:) which both return a DateInterval object containing start, end and duration

let now = Date()
let calendar = Calendar.current
let weekEndInterval = calendar.dateIntervalOfWeekend(containing: now) ?? calendar.nextWeekend(startingAfter: now)!
let startDate = weekEndInterval.start
let endDate = startDate.addingTimeInterval(weekEndInterval.duration-1)
print(startDate, calendar.startOfDay(for: endDate))
vadian
  • 274,689
  • 30
  • 353
  • 361
  • 1
    Alternatively: `let weekEnd = cal.dateIntervalOfWeekend(containing: now) ?? cal.nextWeekend(startingAfter: now)!` – the latter would fail only if there are no weekends in the current calendar/locale. – Martin R Aug 04 '18 at 07:04
1
let (nextSaturday,orderTotal) = getWeekends()
print(nextSaturday)
print(nextSunday)

func getWeekends() -> (Date,Date) {
        let today = Date()
        let calendar = Calendar.current

        let todayWeekday = calendar.component(.weekday, from: today)

        let addWeekdays = 7 - todayWeekday
        var components = DateComponents()
        components.weekday = addWeekdays

        let nextSaturday = calendar.date(byAdding: components, to: today)
        components.weekday = addWeekdays + 1
        let nextSunday = calendar.date(byAdding: components, to: today)

        return (nextSaturday,nextSunday)
    }
Tm Goyani
  • 406
  • 2
  • 9