Change iOS Datepicker Display Format - ios

How can I change the iOS Datepicker date selected display format? The format displays as 2015-12-30. I want the date to display as 12/30/2015.

Ok, as I told you in comment above. Here is how you can get the date in string from UIDatePicker
in Objective-C
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
NSString *stringFromDate = [formatter stringFromDate:self.yourDatePicker.date];
in Swift
let dateFormater: NSDateFormatter = NSDateFormatter()
dateFormater.dateFormat = "MM/dd/yyyy"
let stringFromDate: String = dateFormater.stringFromDate(self.yourDatePicker.date) as String

I have set up my datePicker in Storyboard. In Swift 5, in order to get dates in correct format, you should do the following:
Specify datePicker's locale in Storyboard (that would translate months' names to your language).
Create dateFormatter like #Cong Tran wrote. Though now you could use DateFormatter instead of NSDateFormatter.
Convert string back to date using date(from:) method using optional binding.
Assign formatted date to datePicker's date.

Related

ios: how to change date-picker display format?

I'm new in iOS.
How to change date-picker display format ?
My requirement is like below image.
Give idea or suggestion
Thanks
In Apple's documentation they specified that in UIDatePickerModeDate. The DatePicker displays months, days of the month, and years. This order items depends on your device local setting. you can check this documentation Date Picker Description
if you want to same date picker then you could create your own picker.
It's not possible with native UIDatePicker you need to use the custom one for e.g UIPickerview
Try this :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:"pass your string date"];
[dateFormatter setDateFormat:#"EE, dd MMM, yyyy hh:mm a"]; // change format as per your needs
NSString * strTimestamp = [dateFormatter stringFromDate:date];
NSLog(#"your stirng%#",strTimestamp);
let dateFormatter = DateFormatter()
let todayDate:NSDate = NSDate()
dateFormatter.dateFormat = "dd MMM yyyy"
let str:String = dateFormatter.string(from: todayDate as Date)
print("String is ----%#",str)
You can write as this Sender date of your date picker
//let str: String = dateFormatter.string(from: sender.date)
Out put
String is ----%# 24 Apr 2017

NSDateFormatter dateFromString incorrect

I am setting the value of a UITableViewCell's detailTextLabel to the current date and am then allowing the user to modify that date using a UIDatePicker. I set the text label like this
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a MM/dd/YYYY"];
cell.detailTextLabel.text = [dateFormatter stringFromDate:currentDate];
This sets the text label properly, and it displays something like 08:20 AM 08/07/2015. This is the desired output. When the user selects this cell, I want the date property of my date picker to be set to the date displayed in the cell. To implement this, I have the following in my tableView:didSelectRowAtIndexPath: method
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a MM/dd/YYYY"];
NSString *dateString = [tableView cellForRowAtIndexPath:indexPath].detailTextLabel.text;
self.datePicker.date = [dateFormatter dateFromString:dateString];
However, rather than setting the date to the one that is in the cell, this sets the date of the picker to 08:20 AM 12/21/2014, which is not what it should be. Logging dateString outputs the correct string that is in the table view cell.
Is there a reason that I am experiencing this issue?
Instead of #"hh:mm a MM/dd/YYYY" formatter try to use #"hh:mm a MM/dd/yyyy".
According to the Apple's docs:
"A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year."
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
The year portion of your formatter string should be yyyy, not YYYY.

Convert a date to English format form Bengali

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.
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...
Get a date from a UIDatePicker.
Render that date into a UITextField in Bengali. (or label, or text view or something)
Trying to read the text and store it into a date.
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;
}

NSDate to String conversion and display in the UILabel

I am trying to get a string from NSDate object type and display it in a label.
Here is the code I am using,
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
//[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *datey = [dateFormatter dateFromString:selectedDate];
NSString *labelData = [dateFormatter stringFromDate:datey];
dateLabel.Text = labelData;
where selectedDate is a String containing date in yyyy-mm-dd format.
The label is not showing up the date. But if I try giving it a string like #"someblah", it is displaying. Where am I going wrong??
Help appreciated. Thanks
Your line dateLabel.Text should be dateLabel.text.
So you start with a date string, convert it to a date, and then convert it back to a string using the same formatter. Isn't it easier to use:
dateLabel.text = selectedDate;
In all likelihood you are trying to create an NSDate using a date string that is inconsistent with the format. For example, if the format is #"yyyy-MM-dd" but your selectedDate differs from that format then the formatter won't return a NSDate. You can avoid this by setting the DateFormat to be correct for the selectedDate and then, once parsed, change the DateFormat for the desired output. Or use two NSDateFormatter instances.
Try specifying a specific format instead of NSDateFormatLongStyle, for example
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
I found this answer in 2 seconds using google.. (Yes thats a hint)

Am I using the correct format pattern for parsing a date?

I have problem using the NSDateFormatter to parse a date string. I have implemented the method method below as an NSDate category. The input is the following date string Thu, 22 Dec 2011 16:03:39 +0100 and using the following pattern for parsing EEE, dd MMM yyyy HH:mm:ss ZZZ
The problem is that the method returns nil
+(NSDate*) dateFromString:(NSString*)dateString pattern:(NSString*)pattern
{
NSDateFormatter *dateParser = [[NSDateFormatter alloc] init];
[dateParser setDateFormat:pattern];
NSDate *parsedDate = [dateParser dateFromString:dateString];
return parsedDate;
}
I have looked on Stackoverflow and elsewhere on the Internet, but not found a solution to this problem.
NSDateFormatter is quite smart about region settings like 12/24 hour display, month and weekday names, etc.
If you initialize a date formatter it always uses the current locale, you can set the locale manually, though. You can use the en_US_POSIX locale identifier to get a date formatter back that doesn't respect locale settings and always uses the same behavior.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
This is handy (and required, actually) if you need to parse date strings returned from a server for example.
If you want to display date strings in your app, you should never use the -setDateFormat: method directly, btw. Use +dateFormatFromTemplate:options:locale: method to get the correct date format.
In some languages the month is written before the day, ...
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:#"dd MMMM yyyy" options:0 locale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:dateFormat];

Resources