I writing an app in Xamarin.iOS but an answer in native will suffice. I want to localize my date picker so that it shows the picker in French. So far I did this:
Locale = new NSLocale("fr-CA");
It shows the month in French, but not the days of the week. Here is an image of the result:
How can I change the days of the week (SUN, MON, etc) to French?
In the bellow code language of the days of week will be changed according to the user preferences:
let datePicker = UIDatePicker()
datePicker.locale = .autoupdatingCurrent
Related
In my app I need to group different items based on their date, so I need a method to get date of the beginning of the week, and also a method to find out if two dates are in the same week. However, I get unexpected problems trying to implement this.
I use this code to get the first day of the week:
return Calendar.current.date(from: Calendar.current.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
I use Calendar.current, but it returns the same day no matter which day is chosen as the beginning of the week in iOS Settings.
To check if two dates are in the same week I use this code:
func isInSameWeek(date: Date) -> Bool { isEqual(to: date, toGranularity: .weekOfYear) }
Still, Sunday and Monday dates are always considered not in the same week no matter which day is chosen as the beginning of the week in iOS Settings.
How do I fix this? Or maybe it is a normal behavior and this code will work correctly for users in different regions?
You are changing the settings of the Calendar App not the iOS current calendar itself. You need to provide your own app settings to be changed by the user.
var calendar = Calendar.current
calendar.firstWeekday = 2
calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))?.description(with: .current) // "Monday, September 14, 2020 at 12:00:00 AM Brasilia Standard Time"
Here is my situation, Current Date picker is showing date, month and year in sequence of Date, Month and Year OR Month, Date and Year. But I want to display this sequence as Year first then Month and at last Date.
I searched a lot but same question I found in Javascript but not in iOS, I also searched in Apple Documentation but I didn't find any solution.
How can I achieve this sequence Year, Month and Date? If there any property or method available for DateTime Picker? OR Do I need to use custom picker? if any then which?
Thanks.
The order of year, month and day is depending on the picker's locale.
Eg.
datePicker.locale = NSLocale(localeIdentifier: "zh_TW")
will have different order from
datePicker.locale = NSLocale(localeIdentifier: "en_US")
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.
I'm trying to have a UIDatePicker to present only the date, month and year to the user. I want to avoid presenting them with the hours, minutes and seconds like it comes by default. I've tried looking for something that might help but can only find solutions that aren't explained clearly in Objective-C, however I'm coding in Swift. Any ideas? Thanks in advance!
you need to chose the mode of the date picker
#IBOutlet weak var myDatePicker: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
myDatePicker.datePickerMode = .date // or .Date (Swift 2.x)
}
date picker options .
Declaration SWIFT
var datePickerMode: UIDatePickerMode
Discussion The
value of this property indicates the mode of a date picker. It
determines whether the date picker allows selection of a date, a time,
both date and time, or a countdown time. The default mode is
UIDatePickerModeDateAndTime. See Date Picker Mode for a list of mode
constants.
UIDatePickerMode
The mode of the date picker.
Declaration Constants
Time
The date picker displays hours, minutes, and (optionally) an AM/PM designation. The exact items shown and their order depend upon the locale set. An example of this mode is [ 6 | 53 | PM ].
Available in iOS 2.0 and later.
Date
The date picker displays months, days of the month, and years. The exact order of these items depends on the locale setting. An example of this mode is [ November | 15 | 2007 ].
Available in iOS 2.0 and later.
DateAndTime
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 ].
Available in iOS 2.0 and later.
CountDownTimer
The date picker displays hour and minute values, for example [ 1 | 53 ]. The application must set a timer to fire at the proper interval and set the date picker as the seconds tick down.
Available in iOS 2.0 and later.
If you're using storyboards just change mode to "date" in the Attributes inspector.
YourPickerView.datePickerMode=UIDatePickerModeDate;
I have dataPicker in my app implemented. But it only shows month,day,hour. How could I able to add year in the date picker. Any idea?
datePicker.datePickerMode = UIDatePickerModeDate;
UIDatePickerModeDate
The date picker displays months, days of the month, and years. The exact order of these items depends on the locale setting. An example
of this mode is [ November | 15 | 2007 ].
Available in iOS 2.0 and later.
Declared in UIDatePicker.h.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html
Also you can change from storyboard set Mode: (Refer to screenshot)
If you want to add month, date, year as well as time, add one picker that implements the dates only using (UIDatePickerModeDate) and an additional one for the time (UIDatePickerModeTime).