How to change Date Format in UIDatePicker - ios

I got stuck that I don't know how to change Date Format in UIDatePicker? As default, current dateformat in UIDatePicker is December 23 2015. What I want is I want to change 23 Dec 2015.
Any solution will be appreciated.

The displayed date format of the UIDatePicker is depending on the locale settings of the UIDatePicker.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDatePicker_Class/#//apple_ref/occ/instp/UIDatePicker/locale
To change it, you have to set an NSLocale, which prefers your format.

Related

JTCalendar showing wrong date in Calendar

I make JTCalendar into custom view , all thing were working fine until I change timezone to EST, the calendar is showing 27 days for February but the actual number of days in 2018 is 28.
It happen when I start the app in default timezone and then changing the timezone to New York from settings, then when I open calendar, layout is wrong. After this when I tap into date 24 and result is date 23. only happen when I change timezone from IST to EST.

Incorrect locale from dateFormatter

I have problem with my DateFormatter.
My iOS app communicates with server and uses If-Modified-Since header with date created with following formatter:
modifiedSinceDateFormatter = DateFormatter()
modifiedSinceDateFormatter!.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
modifiedSinceDateFormatter!.locale = Locale(identifier: "en_US")
modifiedSinceDateFormatter!.timeZone = TimeZone(abbreviation: "GMT")
It works as expected - returning date in following format: Fri, 08 Sep 2017 07:02:20 GMT.
But I was looking through the server logs and found that once request was made with following date format sob., 26 sie 2017 10:17:01 CEST (that's correct Polish locale and timezone - I expect my users to use Polish locale).
So my question is:
How is it possible that this formatter returned date in the wrong locale? Are there some options that user can activate to override this locale (like Accessibility options)? Can it be some jailbroken device?
EDIT: And it happened again: wt., 17 kwi 2018 08:40:02 CEST. Interesting that there was few requests (at same moment from single device) and only one of them failed - with wrong date).
I found the following explanation on how to ensure you get your date properly parsed using English names for months and days
Unless you specifically need month and/or weekday names to appear in the user's language, you should always use the special locale of en_US_POSIX. This will ensure your fixed format is actually fully honored and no user settings override your format. This also ensures month and weekday names appear in English. Without using this special locale, you may get 24-hour format even if you specify 12-hour (or visa-versa). And dates sent to a server almost always need to be in English.
I found the quote on this page

How to automate UIDatePicker to select a past date using KIF framework

I have a date picker with these 4 columns "Thu Jul 23, 10, 20, AM”.
I want to automate date picker using KIF to select a past date like Wed Jul 15,10,20,AM. I could select Jul 15 but with unknown year resulting in wrong weekday.It supposed to be "Wed Jul 15" but resulting "Mon Jul 15". I want to select a particular past date in the current year(2015).This is my current working code.Any help is greatly appreciated.
NSArray *pastDate = #[#"Jul 15", #"", #"",#"”];
[tester selectDatePickerValue:pastDate];
Finally, I did some work around to select past and future dates in the date picker using KIF framework. Instead of setting date picker value to past/future date ,I am trying to scroll the date picker to past/future date.Here is the code snippet
- (void)testSelectPastDate{
[tester scrollViewWithAccessibilityIdentifier:#"datePicker" byFractionOfSizeHorizontal:0.1 vertical:0.3];
}
- (void)testSelectFutureDate{
[tester scrollViewWithAccessibilityIdentifier:#"datePicker" byFractionOfSizeHorizontal:0.2 vertical:-0.2];
}
you can adjust the vertical factor based on your requirement

Datepicker date format issue

I am trying to select a date from DatePicker. It works properly often but when i select future date, the picker sets itself to current date(as per my code of maxdate). In this date shows current date in label but at backend object it is actually 1 day minus of current date. e.g After auto set of picker to current date, it displays in label 09-Apr-2015 but in my object(nsdate) which is want to Post to server api, it shows 2015-04-08 18:30:00 +0000. Thus my object send 8 apr to the server. Kindly reply if someone has faced this problem.
This is not a problem. The date picker is giving you correct time but only in the other time zone (Appears GMT+5:30)
When you are sending the date in your API, convert it to NSString using NSDateFormatter of the default time-zone or time-zone of your choice.
Some thing is wrong with your GMT settings.
try this code to fix:
[yourdataobject dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
This will automatically fix your date in whatever zone your app is running in.

I need an UIPickerView or UIDatePicker whit a specific dateFormat

I need a UIDatePicker or UIPickerView with the following date format:
yyyy-MM-dd HH-mm
Do I have to create it ? or is there some pickerStyle or pickerMode that implements it?
You can do this simply by using a UIDatePicker and then set the pickerMode for it. Since, you need both Date as well as Time, all you have to do is set the mode to UIDatePickerModeDateAndTime
UIDatePicker *datePicker;
timePick.datePickerMode =UIDatePickerModeDateAndTime;
The following is provided in the Apple documentation for the above mode-
The date picker displays dates (as unified day of the week, month, and
day of the month values) plus hours, minutes, and (optionally) an
AM/PM designation. The exact order and format of these items depends
on the locale set. An example of this mode is [ Wed Nov 15 | 6 | 53 | PM ].
EDIT:
In your case, I think the only option you have is to implement two UIDatePickers, one with mode set as UIDatePickerModeDate and another with UIDatePickerModeTime. Then, you can concat them both into an NSDate.
Hope this helps!!

Resources