I am trying to trigger specific behaviour based on the season of the year. Eventually I will need this to be localized, but am presently trying to circumvent the issue of the year value being hard coded, trying to always get the current year, but for some reason it will not work:
let currentDate = Date() let formatter = DateFormatter() let formatter2 = DateFormatter() formatter.dateFormat = "yyyy/MM/dd" let currentYear = formatter.string(from: currentDate) //get current year let yearDataInStringFormat = String(currentYear) var globalSeasons: [String : [String : Bool]] = [:] let someDate1 = formatter.date(from: "2020/September/08") ?? Date() let someDate2 = formatter.date(from: "2020/October/08") ?? Date() let checkDate = (someDate1 ... someDate2).contains(Date()) globalSeasons["Europe"] = ["Summer" : ((formatter.date(from: "\(yearDataInStringFormat)/June/01") ?? Date()) ... (formatter.date(from: "\(yearDataInStringFormat)/August/27") ?? Date())).contains(Date()), "Autumn" : ((formatter.date(from: "\(yearDataInStringFormat)/August/28") ?? Date()) ... (formatter.date(from: "\(yearDataInStringFormat)/November/05") ?? Date())).contains(Date())]
I thought it best to keep the data in a nested dictionary, but to perform the bool checks inside it a little cumbersome, and I don't want to commit them to values as that would be a little verbose.
The above code will work with hardcoded values with "checkDate", but I am looking to remove the dependency on the year by substituting yyyy with the dynamic "currentYear" in the dictionary, but it does not work (defaulting to the current date).
Ultimately I would like to check the local season when the app starts.
nb. while it is summer in middle of the year in Europe, it is summer in December in Japan