set specific date in Date Picker IOS - ios

I have a Date Picker on my story board but I don't want it to default to today's date. I want the date to start at July 15 1998. In the attribute inspector i set the date to that and it shows it on my story board but when I launch the application it is still on today's date. Is there something else I have to do for it to work?

[yourDatePicker setDate:[NSDate dateWithTimeIntervalSince1970:900478800]];
or if you want to be fancy:
[yourDatePicker setDate:[NSDate dateWithTimeIntervalSince1970:900478800] animated:YES];
you can put this in your viewDidLoad:

Related

swift xcode UIDatepicker

How do I get data picker to display and select the year also.
Currently I see the following but what I need is Year Mon Day HH MM
.datePickerMode = .date or .datePickerMode = .dateAndTime
both are dependant on locale... so different countries will see different potentially undesirable formats.
Swift Accessing year from UIDatePicker
You can customise UIPickerView though if you need to...
Can i customize UIDatePicker?

Datepicker maximumDate stuck on yesterday day during early time of the current day

I have a date picker that is set to not allow users to pick future dates. It's maximum date is set for the current time on viewDidLoad.
dateDatePicker.maximumDate = NSDate()
But I've been noticing a bug. Today until midday the maximum date was yesterdays date. But now, after midday it is showing todays date.
The way I implemented it: The date picker is shown and hidden with alpha when user wants to use it. So I am setting the maximum date not every time the picker is shown but once it loads the view it is in.
So I am not sure if this is a bug?
Is it has to do with timezones?
Or has to do with the fact that I am only calling dateDatePicker.maximumDate once when they view loaded and that could have happened days ago?
Or if I should reset the maximumDate every time I show the datePicker?
Yes, you should almost certainly set the maximum date each time you show the date picker.
If you set the maximum date on a previous day and then never updated it nor closed the view controller the it wouldn't change.
Set it in viewWillAppear, not viewDidLoad.
If that doesn't work, use NSCalendar and NSDateComponents to build an NSDate which is 23:59 on the current date (fetch the MM/DD/YYYY from the current date as date components, then manually set the time to 23:59. That should work.

Second date Picker should show dates after the date selected in first date picker

I have two date pickers. In first I'm selecting a date, and I want second date picker to show the dates after that particular date. Previous dates should be disabled.
Use minimum date property of the date picker to disable the all date before the minimum date. The date picker would also allow you to define range as minimum and maximum date so that it can show the date from minimum to maximum date range and remaining all other date will be disabled in picker view.
swift 3
datePickerEndDate.minimumDate = datePickerStartDate.date

DatePicker issue in swift

I am new to Swift. In my app, I have a date picker concept. My issue is when select date button, date picker is appearing fine suppose I select DEC 20 2015. My issue is when scroll date picker wheel very fast and press done button on my date picker tool bar date is not picking . If I open date picker I want to show previously selected date example DEC 20 2015. But it shows some other dates when I scroll faster.
var datePicker:UIdatePicker
func dateselcted()
{
var date:NSDate = NSDate()
datePicker.minimumdate = date
if cell.dateText.characters.count == 0
{
//If user not select any date I am showing current date and working fine
var dateOb:AnyObject =
datePicker.setdate(date ,animated:false)
}
else
{
// Suppose user select DEC 20 2015 storing this date in one object example below and working fine but it is fails when scroll datepicker wheel very fast then it is showing the other date how to show "DEC 20 2015 " when picker wheel scrolls fastly I want to show date what I selected
var dateOb:AnyObject = DEC 20 2015
datePicker.setdate( dateOb as nsdate ,animated:false)
}
}
A date picker only invokes it's action method when the wheels stop spinning. You don't get called continuously as the wheels spin from value to value.
It doesn't look like there is any facility to do what you want (get notified of value changes as the wheels in the date picker spin.)
Furthermore, I don't think the value of a date picker (or a regular UIPickerView) changes until it stops spinning, so it will still have it's old value if you start it spinning and click the button before it stops.
Are you waiting for the scrolling to stop before pressing your done button? That may be your issue.
This answer may have what you are looking for.
The delegate
pickerView:didSelectRow:inComponent:
is not called until the picker stops spinning. If you need to determine the selected row while spinning, you will need to write some logic to guess the state when done is pressed.
There is no way to select a date like you do and still expect the date picker picking your desired date automatically. The device does not know what we think. Normally, users won't press a button until they see that date gets selected. So, I think your concern is trivial.
You can first set datePicker.date to [NSDate date], then set datePicker.date to previously selected date. the OC code for you:
datePicker.date = [NSDate date];
datePicker.date = date;
It works for me, but I don't know why.

UIDatePicker customize available time intervals

Is it possible to set in UIDatePicker, date/time frames selectively? for example: show only Mondays from 8am to 4pm ?
Unfortunately, you cannot do this with standard UIDatePicker. Instead you should build your own custom date picker based on UIPickerView.

Resources