-7

Context

I'm making an app where I have alarm and I need to show the time remaining to next alarm.

Problem

How do I get the array of selected Dates?

Example

User selected MO TU WE TH FR SA SU and time 07:30. Now I need to get Dates for those 7 days with time 07:30. It should work with year and month changing. For example "MO TU WE TH" can be 2018, but FR SA SU can be 2019.

Also, I need an option to create Dates not from all weekdays, but from several, for example only for "TU WE SA"

Marksss
  • 11
  • 3
  • The other way round: Work with `Date`s and show their weekday component to the user. `Calendar` provides all methods to do the date math. – vadian Apr 22 '18 at 12:07
  • @vadian with this way i am still need array of 'Dates' – Marksss Apr 22 '18 at 12:18

2 Answers2

0

How do you want the user to pick the weekdays?

Stijnk008
  • 222
  • 3
  • 23
0

Recently I was working on a app related to calendar here I created this function to get week days from current day.

func getWeek(date:Date) -> [Date]? {
    var dateComponents = Calendar(identifier: .gregorian).dateComponents([.yearForWeekOfYear, .weekOfYear], from: date)
    dateComponents.timeZone = TimeZone.current
    let startOfWeek = Calendar(identifier: .gregorian).date(from: dateComponents)!
    let startOfWeekNoon = Calendar(identifier: .gregorian).date(bySettingHour: 0, minute: 0, second: 0, of: startOfWeek)!
    let weekDays = (0...6).map { Calendar(identifier: .gregorian).date(byAdding: .day, value: $0, to: startOfWeekNoon)! }
    return weekDays
}
Hritik Singh
  • 101
  • 7