How to set weekdays date in CollectionView starting from Monday to Saturday in swift

swift weekday
swift get first day of week
swift get next monday
swift calendar
weekday ordinal swift
swift get next day of week
swift date components
getting day from date in swift

I have to show weekdays date in CollectionView start from Monday to Saturday. Until the end of the week, I have to show that weekdays date only. My task image Image. Please help/advise me how to do this task.

I am getting weekdays but its start from current day, But i need start date from monday,

func arrayOfDates() -> NSArray {

        let numberOfDays: Int = 6
        let formatter: DateFormatter = DateFormatter()
        formatter.dateFormat = "dd"
        let startDate = Date()
        let calendar = Calendar.current
        var offset = DateComponents()
        var dates: [Any] = [formatter.string(from: startDate)]

        for i in 1..<numberOfDays {
            offset.day = i
            let nextDay: Date? = calendar.date(byAdding: offset, to: startDate)
            let nextDayString = formatter.string(from: nextDay!)
            dates.append(nextDayString)
        }
        return dates as NSArray
    }

Try this

func formattedDaysInThisWeekNet() -> [String]
{
    // create calendar
    let calendar = NSCalendar(identifier: NSCalendar.Identifier.gregorian)!

    // today's date
    let today = NSDate()

    let weekday = calendar.component(.weekday, from: today as Date)
    let beginningOfWeek : NSDate
    if weekday != 2 { // if today is not Monday, get back
        beginningOfWeek = calendar.nextDate(after: today as Date, matching: .weekday, value: 1, options: [.matchNextTime, .searchBackwards])! as NSDate
    } else { // today is Monday
        beginningOfWeek = calendar.startOfDay(for: today as Date) as NSDate
    }
    var formattedDays = [String]()
    for i in 0..<7 {
        let date = calendar.date(byAdding: .day, value: i, to: beginningOfWeek as Date, options: [])!
        formattedDays.append(formatDate(date: date as NSDate))
        let firstDate = calendar.date(byAdding: .day, value: 0, to: beginningOfWeek as Date, options: [])!
        let lastDate = calendar.date(byAdding: .day, value: 6, to: beginningOfWeek as Date, options: [])!
        let fullString = "\(formatDateFull(date: firstDate as NSDate)) - \(formatDateFull(date: lastDate as NSDate))" as String
        fulldateLbl.text = "<  \(fullString)  >"
        print(fullString)
    }
    return formattedDays
}

weekday, set weekdays date in CollectionView starting from Monday to Saturday in swift Try this func formattedDaysInThisWeekNet() -> [String] { // create calendar let  The Minute function returns the minute component of a Date/Time value, ranging from 0 to 59. The Second function returns the second component of a Date/Time value, ranging from 0 to 59. The Weekday function returns the weekday of a Date/Time value. By default, the result ranges from 1 (Sunday) to 7 (Saturday). You can specify a different range

enum Days: Int {
    case Sat = 0, Sun, Mon, Tue, Wed, Thu, Fri
    static var all = [Mon, Tue, Wed, Thu, Fri, Sat]
}

func getWeekDays(date: Date) -> [Date] {
    var weekDates = [Date]()

    let cal = Calendar.current
    var comps = cal.dateComponents([.weekOfYear, .yearForWeekOfYear], from: date)
    let days = Days.all
    for day in days {
        comps.weekday = day.rawValue
        weekDates.append(cal.date(from: comps)!)
    }
    return weekDates
}

    print(getWeekDays(date: Date()))  // print all dates from Monday to Saturday 

Get the date of the next Sunday? : swift, weekday. Language: Swift, Objective-C. Language: Swift; Objective-C var weekday: Int { get set }. Discussion. Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1. The ISO 8601 week date of the year. There are a variety of functions to work with days of year and days of the week in Excel, including WEEKDAY, DAY, DAYS, DATE and more. These functions can be used to determine workdays and weekends, calculate the number of days between two dates, count the number of days remaining in the year, return day of the week from date, and much more.

Hi guys thank you for your response. I completed This task.

func arrayOfDates() -> NSArray {

    var calendar = Calendar(identifier: Calendar.Identifier.gregorian)
    let today = calendar.startOfDay(for: Date())
    let dayOfWeek = calendar.component(.weekday, from: today) - calendar.firstWeekday
    let weekdays = calendar.range(of: .weekday, in: .weekOfYear, for: today)!
    let dayss = (weekdays.lowerBound ..< weekdays.upperBound)
        .compactMap { calendar.date(byAdding: .day, value: $0 - dayOfWeek, to: today) }
    //.filter { !calendar.isDateInWeekend($0) }

    let formatter = DateFormatter()
    formatter.dateFormat = "dd"

    let strings = dayss.map { formatter.string(from: $0) }
    self.dates = strings as NSArray
    return dates as NSArray
}

But here I have one question, How to disable the previous dates. I need to select only current date and next dates.

Finding start and end of current week (gives me the next week) : swift, Hello dear swifters, i'm building an app for businesses schedule management. based on another date, but all i have is a day of the week: sunday, monday . A completely revamped GUI (I literally started the whole thing again from scratch) etc. to others just like them, I decided to create a new private Facebook Group,  If you want to display your items as categories wise like Flowers and fruits using UICollectionView, xcode 8.2.1 and swift 3.0.This video help you a lot.. Before watching this video I would like

Scheduling daily notifications on iOS using Calendar and , I'm trying to find the start and end date of the week we are currently in. However​, the Gregorian calendar considers Sunday as the start of the week (may differ based you Monday as the first day or set the first weekday property on the calendar itself to specify what Building calendar without UICollectionView in SwiftUI. Also, remember to set your cell backgroundColor in cell contentView. JZBaseEvent. In JZCalendarWeekView, the data model is using [Date: [Event]] dictionary because for each day (a section in collectionView), there might be some events.

FSCalendar/MOREUSAGE.md at master · WenchaoD/FSCalendar , Let's get started with learning about the Calendar , shall we? If you have worked extensively with dates and times on a global scale, you using a DateComponents object where only the hour is set to 9 . will start their week on a Monday, but some might start on a Sunday. Categories: Advent of Swift  This blog helps in displaying the start of the week date span and end of the week date span, which will be displayed, using SQLl query. Here, I used some date time function to find out the week's start and end date simultaneously. It returns a particular date with the particular number interval added to a particular date part of the date.

Showing previous week dates instead of current week in custom Xib , Focus on selected date on week mode If you want FSCalendar to use Monday as the first column (or any other weekday) firstWeekday = 2;. Swift. calendar.​firstWeekday = 2. fscalendar---monday You can define the appearance Like UITableView or UICollectionView, there is also reloadData in FSCalendar ;. iOS(Video):- In this video I will explain you to how to create and used Custom CollectionView. && Also How to Pass CollectionView Data to Another View Controller in Swift 4 And Xcode 9.

Comments
  • Hi Martin R thank you. I edited my code plese check it
  • Possible duplicate of stackoverflow.com/q/42981665/1187415.
  • Yeah, your answer is fine but I need 6 days but here it's giving only 5 days. if I remove .filter { !calendar.isDateInWeekend($0) } it's showing 7 days and its starting from sunday. How to show Monday is start date
  • You also need to set calendar.firstWeekday = 2 to start on Monday. The firstWeekday property tells you what day of the week the week starts in your locale.
  • Thank you teja_D. But it's giving only Monday date. I need 6 days a week
  • Call this function where you want to use weekdays
  • are you able to figure out this? @Ram
  • This line formattedDays.append(formatDate(date: date as NSDate))
  • I completed this task. Thank you teja_D.
  • Thank you @Atif Khan. But i ma getting 7 days . And i need start from Monday
  • I am printing only date like todays date 25 but i need this Date formate: 2018-10-25. If i change this formate i cant show whole formate in collectionViewCell. Plese some one help me.