UIDatePicker show only Sunday's date only?
finally, I myself found solution. In didSelectRow Method check if the selected day is sunday??? if yes then ok but if not then reload component to select date of nearest sunday.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
pickerView.reloadComponent(1)
}
let titleLabel = pickerView.viewForRow(row, forComponent: component) as? UILabel
titleLabel?.font = UIFont(name: BCGConstants.Fonts.Name.ProximaNovaBold, size: 27)!
var dayValue = pickerView.selectedRowInComponent(1) + 1
let monthValue = pickerView.selectedRowInComponent(0) + 1
var yearValue = 0
let unitFlags: NSCalendarUnit = [.Day, .Month, .Year, .Weekday]
let currentDateComponents = NSCalendar.currentCalendar().components(unitFlags, fromDate: NSDate())
if monthValue > currentDateComponents.month || (dayValue >= currentDateComponents.day && monthValue == currentDateComponents.month ) {
yearValue = currentDateComponents.year
} else {
yearValue = currentDateComponents.year + 1
}
debugPrint("\(self.isGivenDaySunday(dayValue, selectedMonth: monthValue, selectedYear: yearValue)) day = \(dayValue) month = \(monthValue) )")
let sundayCheck = self.isGivenDaySunday(pickerView.selectedRowInComponent(1) + 1, selectedMonth: pickerView.selectedRowInComponent(0) + 1, selectedYear: yearValue)
if sundayCheck.isSunday {
self.startDateTextField.text = sundayCheck.sundayDate?.fullStyleDateString
self.newBootcamp?.startDate = sundayCheck.sundayDate!
} else {
// titleLabel?.font = UIFont(name: BCGConstants.Fonts.Name.ProximaNovaBold, size: 27)!
// titleLabel?.textColor = UIColor.lightGrayColor()
if dayValue > 15 {
dayValue = pickerView.selectedRowInComponent(1) - (7 - sundayCheck.nextSundayAsWeekDay)
pickerView.selectRow(dayValue, inComponent: 1, animated: true)
} else {
dayValue = pickerView.selectedRowInComponent(1) + sundayCheck.nextSundayAsWeekDay
pickerView.selectRow(dayValue, inComponent: 1, animated: true)
}
var confirmSunday = self.isGivenDaySunday(dayValue + 1, selectedMonth: monthValue, selectedYear: yearValue)
// Added by mohsin : Reason bug : selecting previous day
if confirmSunday.sundayDate?.isLessThanDate(NSDate()) == true {
confirmSunday = self.isGivenDaySunday(dayValue, selectedMonth: monthValue, selectedYear: yearValue + 1)
//TODO: Need to be verify again : If not working fine then you must try to change next commented statement and uncomment it
// dayValue = pickerView.selectedRowInComponent(1) + sundayCheck.nextSundayAsWeekDay
pickerView.selectRow(dayValue - 1, inComponent: 1, animated: true)
}
self.startDateTextField.text = confirmSunday.sundayDate?.fullStyleDateString
self.newBootcamp?.startDate = confirmSunday.sundayDate!
debugPrint(confirmSunday.sundayDate?.fullStyleDateString)
}
}
Method which checks sunday is following one
func isGivenDaySunday(selectedDay: Int, selectedMonth: Int, selectedYear: Int) -> (isSunday: Bool, nextSundayAsWeekDay: Int, sundayDate: NSDate?) {
let unitFlags: NSCalendarUnit = [.Day, .Month, .Year, .Weekday]
let selectedDateComponents = NSDateComponents()
selectedDateComponents.month = selectedMonth
selectedDateComponents.day = selectedDay
selectedDateComponents.year = selectedYear
let selectedDate = NSCalendar(identifier: NSCalendarIdentifierGregorian)?.dateFromComponents(selectedDateComponents)
let newSelectedDateComponent = NSCalendar.currentCalendar().components(unitFlags, fromDate: selectedDate!)
if newSelectedDateComponent.weekday == 1 { // 1 means SUNDAY as per Gregorian
return (true, 0, selectedDate)
} else {
return (false, 8 - newSelectedDateComponent.weekday, nil)
}
}
How to show only seven days from now in UIDatePicker in iOS?
[datePicker setMinimumDate:[NSDate date]];
[datePicker setMaximumDate:[NSDate dateWithTimeIntervalSinceNow:60*60*24*7]];
He wants only seven days including current date.
how to make date only show weekends (saturday and sunday) for a month?
It is not clear what date type you are using but numpy has some nice business day functionality;
import numpy as np
date = np.datetime64("today")
dates = np.arange(date, date + 31)
mask = ~np.is_busday(dates)
print(dates[mask])
Need to show only Weekday with Date UIDatePicker in iPhone
No, as far as I know there is no way to set a custom date format in UIDatePicker. Anyways, it wouldn't make much sense to display only the weekdays in a date picker, because you would end up with multiple entries for each weekday:
- Fri (3rd)
- Sat (4th)
- Sun (5th)
- ...
- Fri (10th)
- Sat (11th)
- ...
If you only want the user to pick a weekday (Mon-Sun), you could simply use a UIPickerView. And make sure you don't hard-code the weekday names, but rather use - (NSArray *)weekdaySymbols
on NSDateFormatter
. This will take the user's locale into account and returns an array of weekday names as strings:
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray* weekdays = [dateFormatter weekdaySymbols];
// will return Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday (en_US locale)
Alternatively you can use shortWeekdaySymbols
:
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray* weekdays = [dateFormatter shortWeekdaySymbols];
// will return Sun,Mon,Tue,Wed,Thu,Fri,Sat (en_US locale)
How to adjust the date set through UIDatePicker
If what you want to do is to update the UIDatePicker with the date you want, you can simply set the date property of UIDatePicker.
let date = datePicker.date
if let modifiedDate = Calendar.current.date(byAdding: .day, value: 1, to: date) {
datePicker.date = modifiedDate
InputTextField.inputView = datePicker
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
InputTextField.text = dateFormatter.string(from: datePicker!.date)
}
UIdatepicker showing weekdays
You need to create your own picker with a UIPickerView. Create an NSDateFormatter. You can then access the month names and the weekday names from the date formatter. An NSCalendar can be used to obtain the maximum number of days for a month.
The real trick is updating the picker view components as the user selects a value from one of the components. For example, if the user picks a month, you want to update the number of days shown in the day component (or, like a UIDatePicker), grey out invalid days. This is complicated by the fact that you don't have a year so there is no way to properly handle February.
And what do you do when a user picks a given weekday? How should this affect the selected day? Just things to consider.
Related Topics
Enum Named 'Type' in Nested Class Fails
Enum for Buttonstyle in Swiftui
Cannot Decode Object of Class (Ampathpopupbutton)'
How to a Convert a Dictionary Slice to a Dictionary in Swift
The #Selector Is Not Compatible with The Closure
Should I Keep Extensions in Their Own "Extensions" File
Swift Delegate Protocol Cannot Prevent Retain Cycle Issue
Ibdesignable View Not Rendering
How to Make Nsbutton Title at Bottom and Centered
Destructuring Tuple of Tuple in Closure
Swift Cannot Convert The Expression's Type 'Void' to Type 'string!'
Is There Any Reasonable Way to Access The Contents of a Characterset
How to Condense Unwrapping Multiple Optionals in Swift
Confusing About @Environmentobject in Swiftui
How to Group Items in a Nspopupbutton
Swift Codable: Decode Dictionary with Unknown Keys
Errors Encountered While Discovering Extensions: Error Domain=Pluginkit Code=13 "Query Cancelled"
How to Calculate Quadrangle for Visible Part of Vertical Plane