Convert a date to English format form Bengali

1.2k views Asked by At

I have a date picker in my app. The phone is set to Bangladesh local settings. When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.

Like, it returns ০৬/১১/২০১৪

but I want it to be 06/11/2014.

I've tried converting it by date formatter. This is what I tried:

    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[NSLocale currentLocale]];
    NSDate* date = [formatter dateFromString: self.birthDate.text];

    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
    [formater setDateFormat:@"dd/MM/yyyy"];
    NSLog(@"%@",[formater stringFromDate:date]);

The output is null.

1

There are 1 answers

1
Fogmeister On

You are incorrect in your assumption when you say...

When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.

UIDatePicker returns an NSDate object. NSDate has no formatting at all. It has no language, it is purely a point in time.

When you do this...

NSLog(@"%@", someDate);

The system will render that point in time into a string and then print it. It is the rendering into a string that contains the format.

I'm guessing what you are doing is this...

  1. Get a date from a UIDatePicker.
  2. Render that date into a UITextField in Bengali. (or label, or text view or something)
  3. Trying to read the text and store it into a date.
  4. Trying to then "convert" the date to an English string.

What you should be doing is just saving the date that comes from the date picker.

Put it into a property or something.

In your above code I think the bit that is failing is actually the first bit...

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: self.birthDate.text];

Because you're not giving it a format it will fail. But this is the wrong way to go about it anyway.

You should have something like this...

- (void)datePickerChoseADate
{
    self.date = self.datePicker.date;
}