I want to convert string to date. Its not working in following situation.
I am using settings application to set region. In settings, go to general, go to international, go to region format, go to spanish and then set Argentina.
After setting these parameter, I try to convert string to date. Its not converting string to date. Please help.
EDIT : This is my code.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:#"MM/dd/yyyy h:mma"];
NSDate *_date = [formatter dateFromString:strDate];
This document explains what you're experiencing
In short, format your date with the american locale, if you wish to display the date to a user, convert the date to a string.
For example:
NSString *strDate = #"05/29/2012 6:15PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:#"MM/dd/yyyy h:mma"];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
NSDate *_date = [formatter dateFromString:strDate];
And to show the date to a user:
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *newDate = [formatter2 stringFromDate:_date];
Date strings entering the application should typically be converted to a standard format, should you ever want to display a date to the user, you would need to format the date back to a string - think of them as seperate steps:
Get the string to a sane date format
Get the date to the users locale with the desired formatting
When you are converting a String to Date, the String must be in Spanish(Argentina) format for the NSDateFormatter to understand.
in your example, the date you passed is "05/29/2012 6:15PM"
However, in Spanish(Argentina), the date string should be "05/29/2012 6:15p.m."
If you are getting this date dynamically, best is to remove the AM/PM component in the date and try getting the Date String in 24-Hour format
Related
I want to ask that how i get the device date and time format in my like device setting am/pm or 24 hrs what time and what format is set???
Like if i use this format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"mm-dd-yyyy"];
insertCmd = [insertCmd stringByAppendingString:formatter setDateFormat:#"MM.dd.yyyy"];
it set the date format but i want the setting user set in device?
Try this,
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(#"date = %#",[dateFormatter stringFromDate:date]);
This is for converting current date to string. It should be based on the currentLocale of the device. You dont have to set anything. However I am not sure what you meant by the code in your question.
This is what mentioned in documentation,
The format for these date and time styles is not exact because they
depend on the locale, user preference settings, and the operating
system version. Do not use these constants if you want an exact
format, for example if you are parsing an external data file which
contains date information in a fixed format. There are several
different “lengths” of the formats:
Or:
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setFormat:#"mm-dd-yyyy"];
NSString* dateString = [formatter stringFromDate:date];
insertCmd = [insertCmd stringByAppendingString:dateString];
I am converting date from NSString to NSDate using following code
NSString *dateString = #"julho-29-2012 05:01 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm:ss a"];
NSDate* dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#",dateFromString);
[dateFormatter release];
But it not working for me :(
That's because the formats don't match.
"julho-29-2012 05:01 PM"
Follows the following format:
"MMMM-dd-yyyy hh:mm a"
Basically your date formatter isn't able to understand your string and gives a default value, nil.
Edit
You might need to change the formatters locale since your date strings are not in English but Portuguese.
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"pt-PT"]];
Your date format string doesn't match the date format in the string.
Try:
[dateFormatter setDateFormat:#"MMMM-dd-yyyy hh:mm a"];
[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:#"pt"]];
Setting the locale should only be needed if your phone is not in Portuguese already.
your date formatter's format does not match ur date in string format.
I have this string...
2010-08-24T16:00:00-05:00
and I'd like to extract the time portion from it (i.e. 16:00) and convert it to its 12-hour equivalent (i.e. 04:00 pm). I'm trying to use NSDateFormatter to accomplish this, but it's not working...
NSDateFormatter* dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"hh:mm a"];
NSDate *date1 = [dateformatter dateFromString:[listOfTimes objectAtIndex:0]];
[dateformatter release];
Can I use NSDateFormatter with this date format? If not, how can I extract the time and convert it to its 12-hour time equivalent?
Thanks!
The problem has to do with parsing the colon. I asked the same question and the solution is here: How to parse a date string into an NSDate object in iOS?
I think you should be able to do something like the following.
// create the date formatter object
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate* date = [formatter dateFromString:dateString];
// set up the new date format
[formatter setDateFormat:#"hh:mm:ss"];
NSString *twelveHourTime = [formatter stringFromDate:date];
[formatter release];
Update: Fixed the dateFormatter string format. I had the line below, but the Z seems to be unnecessary. Timezones always screw me up. :-/
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
This answer needs to be updated. As of iOS 10 the system provided NSISO8601DateFormatter is available for this particular format.
hello all I have the date in string format "2009-07-06T02:05:11.000+10:00" which i have taken from xml.. Now i need to convert that into date format and use it.How could I do this
Thanks all
See the doc for NSDateFormatter
Roughly as follows:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"DATE FORMAT HERE !!!"];
NSDate *date = [dateFormatter dateFromString: #"DATE TEXT STRING"];
I need to retrieve the date from a UIDatePicker (Preferably I would also like to be specify the format as well. For example, mmdd would output the string 1209. Any string that reasonably parsed would work as well.
Thanks in advance.
You need to use the date property:
NSDate *myDate = datePicker.date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"cccc, MMM d, hh:mm aa"];
NSString *prettyVersion = [dateFormat stringFromDate:myDate];
BTW, it's not obvious but you can add specific non-parsed text by encompassing it inside single quotes in the format:
[dateFormat setDateFormat:#"'Game-'yyyyMMdd-HHmm'.xml'"];
NSString *filenameVersion = [dateFormat stringFromDate:myDate];