I am trying to convert a string which is of the format August 1, 2011 to a NSDate.
I used the following code
NSString *dateStr = #"Aug 3, 2011";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM d, YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];
[picker setDate:date animated:YES];
But the output is get is not the December 26, 2010 in the date picker.
Regarding your comment on Bertrand Marron's answer, if you are sure you have the date formatter right, perhaps the problem is with your picker? Can you check the value of the date in the debugger and confirm that is correct? Is your picker a custom subclass or the standard date picker?
EDIT:
After reproducing this myself, I have the answer.
Replace
[dateFormat setDateFormat:#"MMMM d, YYYY"];
with
[dateFormat setDateFormat:#"MMMM d, yyyy"];
Upper case YYYY indicates "Week of year" in the ISO year-week calendar. See documentation for data formatting guide --> Date formatters for example.
I think you should specify:
[dateFormat setDateFormat:#"MMM d, YYYY"];
Have also a look at this document:
Month - Use one [M] or two for the numerical month, three for the abbreviation, or four for the full name, or five for the narrow name.
(The "narrow name" is something like the initial of the month; you are looking for the abbreviation)
try this it may use
[dateFormat setDateFormat:#"MMM d, YYYY"];
Your date is not formatted like "August 1, 2011".
NSString *dateStr = #"Aug 3, 2011";
The correct format string would be #"MMM d, YYYY"
Although #"MMMM d, YYYY" would work for #"August 1, 2011".
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];// autorelease
[formatter setDateFormat:#"MMM d, YYYY hh:mma"];
Related
One simple thing on conversion from NSString to NSDate. How can I convert Mon, 27 August 2012 01:30 AM to NSDate in this same format. I tried with NSDateFormatter. But I am not getting it in this required format. Can anyone help? This is what I tried.
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE,dd MM yyyy HH:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
NSDateFormatter is to specify the format that will appear in the date-string when extracting string from date or the format that is in the date-string when extracting date from string
So whenever you extract NSDate from a NSString, NSDate is always obtained in default date format(eg 2012-08-27 00:30:00 +0000)... only the when you extract NSString from NSDate, NSString can be obtained in desired(custom) format that you set in NSDateFormatter.
I hope this will help you sure!
NSDateFormatter *dateformater=[[[NSDateFormatter alloc] init]autorelease];
[dateformater setDateFormat:#"EEEE,dd MMMM yyyy HH:mm a"];
NSDate *todayTmp=[NSDate date];
NSString *conversionDate=[dateformater stringFromDate:todayTmp];
Note : (Upper case) HH for 24h time format, (Lower case) hh for 12h time format
NSString *myDateAsAStringValue = #"Mon, 27 August 2012 01:30 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSDate *myDate = [[NSDate alloc]init];
myDate = [df dateFromString:myDateAsAStringValue];
[df release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSString *strDate = [dateFormatter stringFromDate:myDate];
NSLog(#"%#", strDate);
[dateFormatter release];
please use below code
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
your formatter is wrong
check this one
NSLog will return NSDate in a fixed format, i guess.
If we need Date in different format, we should have to format it via NSDateFormatter and get it as NSString.
Just a guess.
Don't forget to set the correct locale! If your device does not use an english locale NSDateFormatter can have problems to convert Mon and August into useful information because Mon is not the correct abbreviation for Monday in your language. For example in Germany the correct three letter abbreviation for Monday is Mon..
If you parse dates that have words in it you have to set the correct locale.
This should work:
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:posixLocale];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
EEE is the dateformatter code for a three letter weekday abbreviation.
hh is the dateformatter code for Hours between 1 and 12. HH means 0-23
MMMM is the full month, MM would be the numeric value (= 08) of the month.
I want to make date by date formatter
2012-07-12 but it display like
2012-07-11
My code:
NString * today_selected=[NSString stringWithFormat:#"%d%#%d%#%d",year_for_activated,#"-",month_for_activated,#"-",taged]; NSDateFormatter *Df=[[NSDateFormatter alloc]init];
//here year_of_=2012 and month_of_ac=7, and tag=12
but it display 2012-07-11 instead of 12.
[Df setDateFormat:#"YYYY-MM-DD" ];
NSDate *date_selected=[Df dateFromString: today_selected];
NSLog(#"today_selected:%#",date_selected);
but it display 2012-01-12
Please read the documentation which states
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.
Also you will note that the day is dd, NOT DD
When you find a problem like this, your first stop should be the documentation
try this:
NString *today_selected=#"2012-07-12";
NSDateFormatter *Df=[[NSDateFormatter alloc]init];
[Df setDateFormat:#"yyyy-MM-dd" ];
NSDate *date_selected=[Df dateFromString: today_selected];
NSLog(#"today_selected:%#",date_selected);
try this for get the 2012-07-12 ,this type of Formatter :
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
In my graph Api for facebook..
I am getting this data..
from Json..
"updated_time" = "2011-05-17T14:52:16+0000";
and I am using this code to convert it into valid date format
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//2010-12-01T21:35:43+0000
[df setDateFormat:#"yyyy-mm-ddHH:mm:ssZZZZ"];
NSDate *date = [df dateFromString:[[forTable valueForKey:#"updated_time"] stringByReplacingOccurrencesOfString:#"T" withString:#""]];
[df setDateFormat:#"eee MMM dd, yyyy hh:mm"];
pastDate = [df stringFromDate:date];
NSLog(#"and the date string is %#",pastDate);
and it is giving me this result in the console
and the date string is Mon 01 17, 2011 08:22
though json value is giving me the date of May.this code is giving me the date January
can anyone tell me what is happening here?
It looks like your date format string isn't taking the letter "T" into account – and you should probably be enclosing literal text between apostrophes, just to be safe. Try:
[df setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZ"];
The dateFormat was wrong, it should be with capital Ms for the month:
[df setDateFormat:#"yyyy-MM-ddTHH:mm:ssZZZZ"];
All i am trying to do, is to get an NSString with the value of the current date ( NOW )
with this format:
7/14/10 8:20 PM
Exactly like the native mail app of the iPhone.
i am using the following code to do it:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd MMMM yyyy HH:mm:ss ZZ"];
[dateFormat setDateStyle:NSDateFormatterFullStyle];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
but the still the result is:
Wednesday, July 14, 2010
Does anyone have any idea of how to get this to just WORK ??
Appreciate your answers..
setDateStyle: overrides the custom format string set with setDateFormat:.
Besides that you should call -setDateStyle: first as per Nikolai answer, your formatting string doesn't match your example, which would be produced by e.g. the following:
[dateFormat setDateFormat:#"M/d/yy h:mm a"];
See the Unicode date format patterns for more details.
See Apple's Date Formatting docs.
I haven't tested it, but try
[dateFormat setDateFormat:#"MM/DD/yy"];
Getting a NSString as follows:
NSString *lastSyncDate = [[items objectAtIndex:0] objectForKey:#"LastSyncDate"];
This value is: 1/1/2010 12:00:00 AM
I am using the following to convert to a date:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeStyle:NSDateFormatterFullStyle];
[df setDateFormat:#"MMMM d, y HH:mm:ss ZZZ"];
NSDate *mySyncDate = [df dateFromString:lastSyncDate];
This gives me a null value?
Your date format doesn't match the format of the string.
Try
[df setDateFormat:#"M/d/y hh:mm:ss a"];
or
[df setDateFormat:#"M/d/y h:mm:ss a"]; // if the hours aren't zero-padded
The format strings are a Unicode standard described at http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
I agree with Seamus Campbell. However, be aware that setting the time style on the formatter (NSDateFormatter) will have no effect on the result returned by dateFromString.