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.
Related
I have a problem with Date Format
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"YYYY-MM-dd hh:mm:ssZZ"];
formatedDate =[format stringFromDate:datePicker.date];
Everything works fine, except one thing
when I set the YEAR less than or equal to 1924, DateFormatter returns
1924-04-21 03:00:07+050748
Whereas should be
1924-04-21 03:00:07+0500
What's wrong?
You can see the Apple Data Formatting Guide https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
In the "Fixed Formats",
There are two things to note about this example:
It uses yyyy to specify the year component. 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.
The representation of the time may be 13:00. In iOS, however, if the user has switched 24-Hour Time to Off, the time may be 1:00 pm.
So you should change "YYYY" to "yyyy".
User this method .......
-(NSString*) currentDateForServer
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:UTC];
[dateFormat setTimeZone:timeZone];
[dateFormat setDateFormat:DATE_FORMATE];
NSDate *currentDate = [NSDate date];
NSString *finalString = [dateFormat stringFromDate:currentDate];
return finalString;
}
Define UTC and DATE_FORMATE as you need
You can set your Timezone, like with GMT,
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd hh:mm:ssZZ"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[format setTimeZone:gmt];
formatedDate =[format stringFromDate:datePicker.date];
This may change your time in output string formatedDate, but you can set NSTimeZone as per your requirement.
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;
}
I am using the NSDateFormatter's dateFromString, yet when it is given the string '2013-03-06' it spits out an NSDate of '2012-03-06 00:00:00 CST'. The time is of no importance. I have the date format set to be exactly the same between the input and output.
I have an object which gets a date set using the code:
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
NSDate* currentDate = [[NSDate alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
[self setMyValue:[dateFormat stringFromDate:currentDate]];
And then this value is later used to to set the date on a UIDatePicker:
NSDate *date = [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
date = [dateFormat dateFromString:myFilter.myValue];
[datePicker setDate:date animated:YES];
Any pointers on where I am erring would be much appreciated.
You should use yyyy and not YYYY.
From Data Formatting Guide:
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
Change YYYY in the format to yyyy.
Use yyyy instead of YYYY, to get the zero error while changing 'YEARS'.
I want to hide the past as well as future dates from my UIDatePicker. Only want to show today's date. Is it possible?
Right now all the dates are coming, but only today's date is selecting. Time is also there. So in short I want to select today's date time by showing today's date only.
Than no need to add DatePicker.
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy"];
dateString = [formatter stringFromDate:[NSDate date]];
I am using the NSDateFormatter's dateFromString, yet when it is given the string '2013-03-06' it spits out an NSDate of '2012-03-06 00:00:00 CST'. The time is of no importance. I have the date format set to be exactly the same between the input and output.
I have an object which gets a date set using the code:
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
NSDate* currentDate = [[NSDate alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
[self setMyValue:[dateFormat stringFromDate:currentDate]];
And then this value is later used to to set the date on a UIDatePicker:
NSDate *date = [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
date = [dateFormat dateFromString:myFilter.myValue];
[datePicker setDate:date animated:YES];
Any pointers on where I am erring would be much appreciated.
You should use yyyy and not YYYY.
From Data Formatting Guide:
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
Change YYYY in the format to yyyy.
Use yyyy instead of YYYY, to get the zero error while changing 'YEARS'.