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"];
Related
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];
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"];
The output comes, for example: 17-10-1990 18:30:00 +0000. When we retrieve the value in NSDateFormatter the day is changed to 16-10-1990. No error comes.
NSDateFormatter *sdayFormat = [[NSDateFormatter alloc] init];
[sdayFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *sdayString = [sdayFormat stringFromDate:datePicker.date];
please help me. I'm new in iphone developer.
to see how to set dateFormatter go to this link - NSDateFormatter and yyyy-MM-dd
and to see the list of all possible formats go to this link
Check this out!
NSString *tDate = #"17-10-1990 18:30:00 +0000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss Z:"];
NSDate *fDate = [dateFormat dateFromString:tDate];
NSLog(#"Given date :%# - Converted Date :%#",tDate,fDate);
use this link as reference for any date related operations
http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/
Hope this helps
I have stuck in issue in which i have to convert date format is Thu, 28 Jul 2011 22:33:57 +0000 into yyyy-mm-dd hh:mm:ss
please give me some idea how to do this?
What you want is to use the NSDateFormatter. Something like this:
NSDateFormatter* f = [[[NSDateFormatter alloc] init] autorelease];
[f setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString* dateString = [f stringFromDate:date];
Do note that the hours will still follow the users selected locale. Use kk:mm:ss to enforce a 24-hour time.
what have you tried? it's difficult to answer questions like this...
first you have to parse the date into an NSDate, use an NSDateFormatter, the incoming format looks like POSIX date format so should be easy.
then you want to output to the format you specify with another NSDateFormatter
You need to use an NSDateformatter to convert the first date to a string with the following syntax.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]]; //Put whatever date you want to convert
Then if you want the date as an NSDate and you have the string generated above just put the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSDate * date = [dateFormatter dateFromString: dateString]; //String generated above
How to convert NSString 2010-08-03 04:37:31.0 to August 3, 2010?
Is it possible?
This is probably a duplicate but NSDateFormatter is what you are looking for.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.S"];
NSDate *date = [dateFormatter dateFromString:inDateString];
[dateFormatter setDateFormat:#"MMMM dd',' yyyy"];
NSString *outDateString = [dateFormatter stringFromDate:date];
There are two problems with this approach though.
The input string is that it lacks timezone data
Different cultures expect a different order than Month Day Year. That can be fixed if you use one of the generic NSDateFormatterStyle formats like NSDateFormatterLongStyle.
Check out the NSDateFormatter documentation. You probably want a format string of #"%B %e, %Y" for the output.