Tuesday, July 16, 2024
HomeiOS Developmentios - How do I make Date Parts of Horizon Calendar in...

ios – How do I make Date Parts of Horizon Calendar in SwiftUI work with in the present day’s date?


Im making an attempt to go in the present day’s date as my begin date in my code and each time I attempt to run a preview my app crashes. Do y’all see what I have to do to repair this?

import SwiftUI
import HorizonCalendar

struct SwiftUIScreenDemo: View {

  // MARK: Lifecycle

  init(calendar: Calendar, monthsLayout: MonthsLayout) {
    self.calendar = calendar
    self.monthsLayout = monthsLayout
    let startDate = calendar.date(from: DateComponents(yr: 2023, month: 01, day: 01))!
      print(startDate)
    let endDate = calendar.date(from: DateComponents(yr: 2026, month: 12, day: 31))!
    visibleDateRange = startDate...endDate

    monthDateFormatter = DateFormatter()
    monthDateFormatter.calendar = calendar
    monthDateFormatter.locale = calendar.locale
    monthDateFormatter.dateFormat = DateFormatter.dateFormat(
      fromTemplate: "MMMM yyyy",
      choices: 0,
      locale: calendar.locale ?? Locale.present)
  }

I attempted breaking all the particular person parts down and getting into them into the datecompnents as integers and ive tried simply including it ass a date. I’ve tried the next:

  init(calendar: Calendar, monthsLayout: MonthsLayout) {
    self.calendar = calendar
    self.monthsLayout = monthsLayout

    // Set the beginning date to in the present day's date
    guard let startDate = calendar.date(from: calendar.dateComponents([.year, .month, .day], from: Date())) else {
        fatalError("Did not create begin date")
    }
    
    // Set the top date to 1 yr from in the present day
    guard let endDate = calendar.date(byAdding: .yr, worth: 1, to: startDate) else {
        fatalError("Did not create finish date")
    }

In addition to

struct DateComponentDetails {
    let currentYear: Int
    let nextYear: Int
    let currentMonth: Int
    let currentDay: Int
    
    init() {
        let in the present day = Date()
        let calendar = Calendar.present
        
        self.currentYear = calendar.element(.yr, from: in the present day)
        self.nextYear = calendar.element(.yr, from: calendar.date(byAdding: .yr, worth: 1, to: in the present day)!)
        self.currentMonth = calendar.element(.month, from: in the present day)
        self.currentDay = calendar.element(.day, from: in the present day)
    }
}

struct SwiftUIScreenDemo: View {

  // MARK: Lifecycle

  init(calendar: Calendar, monthsLayout: MonthsLayout) {
    self.calendar = calendar
    self.monthsLayout = monthsLayout

    let dateDetails = DateComponentDetails()
    
    // Set the beginning date utilizing in the present day's date parts
    let startDateComponents = DateComponents(yr: dateDetails.currentYear, month: dateDetails.currentMonth, day: dateDetails.currentDay)
    guard let startDate = calendar.date(from: startDateComponents) else {
        fatalError("Did not create begin date")
    }
    



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments