swift3 - How to set the first day of current week to Tuesday in Swift? -
how set first day of current week tuesday in swift 3? schedule update every tuesday first day have start tuesday.
and display on label :
thisweekdate.text! = "\(first day of current week) - \(last day of current week)"
full code.
let today = nsdate() let nexttue = calendar.current.date(byadding: .day, value: 6, to: today date) let formatter = dateformatter() formatter.dateformat = "yyyy-mm-dd" let todaystring = formatter.string(from: today date) let nextstring = formatter.string(from: nexttue!) formatter.dateformat = "dd-mmm-yyyy" let calendar = nscalendar(calendaridentifier: nscalendar.identifier.gregorian) let components = calendar!.components([.weekday], from: today date) //label code thisweekdate.text! = "\(first day of current week) - \(last day of current week)" //if can change first day of current week weekly update code can deleted if components.weekday == 3 { print("hello tuesday") thisweekdate.text! = "\(todaystring) - \(nextstring)" userdefaults.standard.set(todaystring, forkey: "todaystringkey") _ = userdefaults.standard.object(forkey: "todaystringkey") as? string userdefaults.standard.set(nextstring, forkey: "nextstringkey") _ = userdefaults.standard.object(forkey: "nextstringkey") as? string } else { print("it's not tuesday") let retriveddate_1 = userdefaults.standard.object(forkey: "todaystringkey") as? string let retriveddate_2 = userdefaults.standard.object(forkey: "nextstringkey") as? string if let date_1 = retriveddate_1, let date_2 = retriveddate_2 { thisweekdate.text! = "\(date_1) - \(date_2)" } }
app design screenshot
create custom gregorian calendar , set first weekday tuesday
var customcalendar = calendar(identifier: .gregorian) customcalendar.firstweekday = 3
then can week interval
var startdate = date() var interval = timeinterval() customcalendar.dateinterval(of: .weekofmonth, start: &startdate, interval: &interval, for: date()) let enddate = startdate.addingtimeinterval(interval - 1) print(startdate, enddate)
wiki
Comments
Post a Comment