I want to change the date formats. But i don't know how to give the input format
`Thu, 2 Dec 2010 00:28:56 -0500' i use the date formatter for user custom format
NSString *inputString = #"Thu, 2 Dec 2010 00:28:56 -0500";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
NSDate *inputDate = [inputFormatter dateFromString:inputString];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"EEE. MMM. d, yyyy"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
mylabel.text = outputDate;
but date not display
please give me the solution
The format string you showed us before you edited your question was correct. The only thing that was missing was that you need to set the date formatter's locale to English if you want it to recognize English month and day names:
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
Related
Output format: date and day format is in the form of 05 March 2012, MondayNSDate *selected = [picker date];
In this code I got output in the form of 1989-07-03, I need to print the corresponding day.
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"yyyy-MM-dd"];
// [formatter setDateFormat:#"dd %b YYYY, %a"];
//Get the string date
NSString* date = [formatter stringFromDate:selected];
//Display on the console
NSLog(#"%#",date);
Use this:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd %b YYYY, %a"];
NSString *theString = [formatter stringFromDate:theDate];
%b is the name of the month, %a is the name of the day
More info here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
dd MM yyyy,EEE like this ?
you can see more info about dateformatter Date_Format_Patterns
use this
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMMM yyyy,EEEE"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
[formatter release];
Instead of using a fixed format, you should chose a style which will be localized:
[format setDateStyle:NSDateFormatterFullStyle];
This way the user will see the date in the date format that he's used to without you needing to worry about it. For example, Americans would get dates like "Tuesday, April 12, 1952 AD" while Germans would get
"Dienstag, 12. April 1952".
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMMM yyyy"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
[formatter release];
I have the following string representing a date & time
NSString *shortDateString = #"Sat28Apr2012 15:00";
(no spaces (apart from a single space in front of the time), just as it is above)
I would like to format this date using:
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
// Question is regarding the line below:
[inputFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm"];
NSDate *formatterDate = [inputFormatter dateFromString:shortDateString];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[outputFormatter setTimeZone:gmt];
[outputFormatter setDateFormat:#"dd.MMM.yyyy HH:mm Z"];
NSString *newDateString = [outputFormatter stringFromDate:formatterDate];
If you change your string slightly to match the format of your input exactly, it works fine. All you need to do is removing spaces and the comma from the string, like this:
"EEEdMMMyyyy HH:mm"
With this string I get the result below:
08.Apr.2012 19:00 +0000
It is 19:00, not 15:00, because my time zone is four hours behind GMT.
[inputFormatter setDateFormat:#"EEEddMMMyyyy HH:mm"];
I know this been asked for so many times but I always end up getting null in my NSDate. I have a string like this "January 16, 2012 21:44:56" I want to convert it to "January 16, 2012 09:44:56 PM". I want to add a PM in the converted date and convert the 24 hour time format to 12 hour time format. Here's my code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:ii:ss a"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
As Ali3n correctly pointed out, you should first set the format of dateString to the formatter to get a valid date object. Next you should set the formatter's format to the desired one and continue. Do the following:
NSString *dateString = #"January 16, 2012 21:44:56";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss"];
NSDate *dateFromString;
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:mm:ss a"];
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
#"MMMM dd, YYYY HH:ii:ss a" this format should match with the date the ypu are passing to the date formatter ..
There is an error in your format string an also you need to tell the formatter the Locale in which your date string is presented.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss a"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release], dateFormatter = nil;
Setting the Local is very important since you have an name of a date in your input. You will need to tell the NSDateFormatter is wich language the name will be. In the example given it is in english. I've you run you code without setting the local on a device where the language is not set to english it wil fail to parse the date.
Try to escape literals in the format string.
[dateFormatter setDateFormat:#"MMMM dd',' YYYY HH:ii:ss a"];
As for your requirements you have to change the dateFormatter.Check this link for more.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
NSLog(#"%#",dateString);
I'm attempting to convert a date from MMM dd, YYYY to YYYY-MM-dd.
NSDateFormatter *importDateFormat = [[NSDateFormatter alloc] init];
[importDateFormat setDateFormat:#"MMM dd, YYYY"];
NSDate *importedDate = [importDateFormat dateFromString:expiresOn.text];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
NSString *dateImported = [dateFormat stringFromDate:importedDate];
The date brought in from the expiresOn.text (label) is:
Oct 08, 2012
The date contained in dateImported after conversion is:
2011-12-25
Am I missing something? Is there something wrong with my date formatter?
Well there are two things wrong, first is yyyy not YYYYY and since you are parsing the month as a word you need to tell the date formatter which language to expect.
NSDateFormatter *importDateFormat = [[NSDateFormatter alloc] init];
[importDateFormat setDateFormat:#"MMM dd, yyyy"];
importDateFormat.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en"] autorelease];
NSDate *importedDate = [importDateFormat dateFromString:expiresOn.text];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSString *dateImported = [dateFormat stringFromDate:importedDate];
If you are using ARC, then remove the autorelease part in the NSLocale; if you are not using ARC, you need to release the NSDateFormatter.
I donno to give the correct date format of the input date, so please give me to correct input date format.
Here my sample code and the date formats.
NSString *yourXMLDate = #"Thursday, September 9, 2010";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"EEEE, MMMM dd, YYYY"]; // It doesnt work
---->[inputFormatter setDateFormat:#"EEEE, ????????????"];
NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"MM/dd"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
Thanks!
You can get the date formatters here
http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/