How to set lunar calendar UIDatePicker in iOS - ios

I need switch UIDatePicker between Solar calendar and Lunar calendar. With Lunar calendar, I can get 02/30/2016 (MM/dd/yyyy), but with Solar calendar, its never get 02/30/2016 (MM/dd/yyyy). So, how can set it in UIDatePicker ?

I don't think it is possible, all supported calendars can be found in Apple Documentation

Lunar Calendar is also known is Chinese Calendar. Try these
self.datePicker.locale = Locale.init(identifier: "zh")
self.datePicker.calendar = Calendar.init(identifier: .chinese)
However, I tried both in simulator and with some app, there is no 2016/02/30 in Lunar. There is 2016/2/29 which convert to 2016/4/6 in Solar. There is 2015/02/30 Lunar (see below)
However, in the date picker, it shows all the leap months even it is invalid.

Related

Calendar days and Time into UIDatePicker with Inline style show only in English

I have made a view with UIDatePicker in Preferred Style "Inline". I need show this calendar in Spanish, so I added Locale lines:
[_pickerCalendar setLocale:[NSLocale localeWithLocaleIdentifier:#"es"]];
_pickerCalendar.calendar.locale = [NSLocale localeWithLocaleIdentifier:#"es"];
With this, I could see that general DatePicker translated correctly but calendar and time label show English yet (see image)
I believed that it could be a iOS bug, but when I saw iOS native calendar it show Spanish correctly (see image)
I can't find which property is the correct to change language. Into Settings - General - Language and Region I hace this information:
Region: Mexico
Calendar: Gregorian
Preferred Language: Spanish
Aditional info:
iOS 14.5 (Simulator) and iOS 14.6 (Device)
Xcode 12.5
Language: Objective-C
Thanks for your help because I really need it.
UPDATE:
I implemented UIDatePicker in Swift with this code:
calendarPicker.locale = Locale(identifier: "es-MX")
calendarPicker.calendar.locale = Locale(identifier: "es-MX")
and the result was days into calendar already shows in Spanish but the label "Time" shows in English yet and not in Spanish. You can see it in the image:

How can i create combine Gregorian and Islamic(Hijri) FSCalendar

Can you please tell me. I changed calendar identifier like this
but it shows a warning (Changing Calendar identifier is not recommended).
_calendar.identifier = NSCalendarIdentifierIslamic;

static let calendar with autoupdatingCurrent time zone is not working

In my application I have a static gregorian property in SharedCalendar class that is defined like this:
static let gregorian: Calendar = {
var calendar = Foundation.Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone.autoupdatingCurrent
return calendar
}()
When I want to access a day of some date in specific time zone I am calling:
SharedCalendar.gregorian.dateComponents([ .day ], from: someDate).day!
Let's say someDate is Date(timeIntervalSinceReferenceDate: 512658000.0) which is 2017-03-31 13:00:00 +0000.
When I start the app in Vancouver time zone the SharedCalendar.gregorian.timeZone property has value America/Vancouver (autoupdatingCurrent) and the result of SharedCalendar.gregorian.dateComponents([ .day ], from: someDate).day! is 31 which is correct.
When I put the application to background and switch the time zone to Sydney and run the app again the SharedCalendar.gregorian.timeZone property has value Australia/Sydney (autoupdatingCurrent) (which is correct), but the result of SharedCalendar.gregorian.dateComponents([ .day ], from: someDate).day! is 31 which is wrong (should be 1).
When I change the definition of gregorian property to be a var:
var gregorian: Calendar {
var calendar = Foundation.Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone.autoupdatingCurrent
return calendar
}
Everything works properly, which is for America/Vancouver (autoupdatingCurrent) I get 31, and for Australia/Sydney (autoupdatingCurrent) I get 1.
Right now I don't quite understand how TimeZone.autoupdatingCurrent is working. When device's time zone changes the SharedCalendar.gregorian.timeZone reflects the device's time zone, but it looks like SharedCalendar.gregorian is somehow using the old time zone.
Does anyone have explanation of this behaviour?
I reported radar regarding this issue and today Apple responded:
The reason that your static let calendar’s time zone doesn’t update is that you need to issue a call to NSTimeZone.resetSystemTimeZone() to sync up with the system time zone. See the documentation for NSTimeZone.resetSystemTimeZone() for more info: https://developer.apple.com/documentation/foundation/nstimezone/1387189-resetsystemtimezone?language=objc
The reason your var calendar works is because every call to the calendar property actually creates a new computed calendar, which happens to be set to a new time zone representing the current system time zone.
This makes sense, because static let caches system's time zone, and from documentation for NSTimeZone.resetSystemTimeZone we can read:
If the application has cached the system time zone, this method clears that cached object. If you subsequently invoke systemTimeZone, NSTimeZone will attempt to redetermine the system time zone and a new object will be created and cached (see systemTimeZone).

iOS UI Test Datepicker

I'm making a UI Test for an iOS app. I'm having trouble finding out how to use automated testing for a date picker. Right now I'm stuck trying to find the date picker itself so I can change the values.
I've searched many threads regarding this issue but have found nothing.
If anyone has any insight to this it would be much appreciated. Thanks!
To get started, I'd advise you to use the "Record UI Test" button in Xcode to easily find out how to access the UIDatePicker in the UI Test.
For a simple UIDatePicker in date mode, changing the date is as simple as:
let datePickers = XCUIApplication().datePickers
datePickers.pickerWheels.element(boundBy: 0).adjust(toPickerWheelValue: "June")
datePickers.pickerWheels.element(boundBy: 1).adjust(toPickerWheelValue: "1")
datePickers.pickerWheels.element(boundBy: 2).adjust(toPickerWheelValue: "2015")
If the datePicker is connected to a UILabel, you could then check if the text in the label has been updated correctly.
For anyone, supporting multiple localizations, an easier way would be
let dateComponent = Calendar.current.dateComponents([.day, .month, .year], from: Date())
let formatter = DateFormatter()
formatter.dateFormat = "MMMM"
let monthText = formatter.string(from: Date())
datePicker.pickerWheels[String(dateComponent.year!)].adjust(toPickerWheelValue: "2017")
datePicker.pickerWheels[monthText].adjust(toPickerWheelValue: "December")
datePicker.pickerWheels[String(dateComponent.day!)].adjust(toPickerWheelValue: "21")

How in iOS to localize text of the month in UIDatePicker, and if it is possible at all?

I make search and up to now I see that UIDatePicker is working with localization of the device.
I have app which has internal lozalization. I need to change month's text dependantly of my internal localization. As I read it appears that this is not possible - I have to make my own custom picker.
Is there a way to achieve that without custom Dat picker?
Use locale property of UIDatePicker. I can't see any signs that the property is deprecated.
This code will localize picker to Japanese.
[datePicker setLocale: [NSLocale localeWithLocaleIdentifier:#"ja"]];
Replace "ja" with any language code you need.
With Swift
let datePicker:UIDatePicker = UIDatePicker()
datePicker.locale = NSLocale.init(localeIdentifier: "it_IT")
Replace the it_IT string (for Italian-speaking people) with your locale identifier. Common cases for english are:
en for generic English-speaking people
en_US for English-speaking residents of the United States
en_GB for English-language speakers in the United Kingdom
For more information about the locale identifier see the Locale IDs section of "Internationalization and Localization Guide" by Apple
If you're interested in getting the current locale see How do I get current application locale?
Swift 3.0 and New
This code will locale picker to Chinese.
Here datePicker is object of UIDatePicker.
datePicker.locale = Locale.init(localeIdentifier: "zh-Hans")
Use "en" instead of "zh-Hans" for English .
Use "az-Arab" instead of "zh-Hans" for Arabic.
Use "uz-Latn" instead of "zh-Hans" for Latin.
for more : click here
The UIDatePicker, by default, uses whatever [NSLocale currentLocale] dictates.
The locale property on UIDatePicker was deprecated in iOS 5, but you can try this:
NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:#"..."];
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setLocale:loc];
[myDatePicker setCalendar:cal];
UIDatePicker localize is dependent on the "Region Format" setting of device.
Settings > General > International > Region Format
I think it is not possible with selecting UIDatepicker control may be you can do it with UIPickerview. please check this link. (you can do it like this way)
UI appearance Datepicker
or
Custom Color of Datepicker
I hope it is helpfull to you.

Resources