Reputation: 3049
My phone's language is Arabic but I want to choose date in english only. I have used calendar as well as locale property of date picker but none is working. I want to get date in English only. My code is as follows:
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setFrame:CGRectMake(0, 100, self.view.frame.size.width, 216.0)];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setLocale: [NSLocale localeWithLocaleIdentifier:@"en_US"]];
[datePicker addTarget:self action:@selector(updateDateField:) forControlEvents:UIControlEventValueChanged];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[datePicker setCalendar:gregorianCalendar];
[self.view addSubview:datePicker];
-(void)updateDateField : (UIDatePicker*)datePicker{
NSLog(@"____ %@",datePicker.date);}
The output is: ٢٠١٤-٠٣-٠٩ ١٠:٣٦:٤١ +0000 but I want the output in english only so that I can store the selected date on the server
Upvotes: 0
Views: 1849
Reputation: 1407
NSLog(@"____ %@",datePicker.date)
will call the datePicker.date.description
by default to format the output string. and the default NSDate
description will use the system default locale that is why you got Arabic output.
-(NSString *)description
is NSObject
's method which is called when a subclass of NSObject
is ‘converted to a string’. NSDate
itself doesn't include any locale information. But NSDate
's description method will create a formatter and use the system's locale.
If you want it to be somewhere in your UI, you should not use default description method, you have to create your own DateFormatter
to format the output with correct locale and format you want.
Alternatively you can make an extension to NSDate
and overwrite description function with your customised DateFormatter
.
Upvotes: 0
Reputation: 1617
You need to use DateFormatter for this. I don't now what format your need send to server. So this is shot example from apple:
let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(forSecondsFromGMT: 0)
let date = Date()
let string = RFC3339DateFormatter.stringFromDate(date)
Upvotes: 3
Reputation: 184
Try using a NSDateFormatter and set the locale to it, then get stringFromDate from the dateController and the date you already have.
Upvotes: 0