How can I get my NSDate to display in the format for example i.e "Tue Feb 26, 2011"
Do it right. Don't hardcode your date formats. There are countries that are not your country and they might have different date formats. So if you want to show this date to the user you should use a method that takes the users locale into account.
You could use the dateFormatFromTemplate:options:locale: method introduced in iOS4 to get the appropriate format with all the information you want.
And if you have to support iOS < 4 you should create a plist with this template method to create the correct date format for the user locale.
NSLocale *locale = [NSLocale currentLocale];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:#"E MMM d yyyy" options:0 locale:locale];
[formatter setDateFormat:dateFormat];
[formatter setLocale:locale];
NSLog(#"Formatted date: %#", [formatter stringFromDate:myDate]);
gives So., 27. Feb 2011 for my locale.
and Sun, Feb 27, 2011 for the en_US locale
NSDateFormatter *gmtFormatter = [[NSDateFormatter alloc] init];
[gmtFormatter setDateFormat:#"E MMM d yyyy"];
Related
I want to display date like Today, 25 Mar 2014 15:18, if the date is today's date.
Also expecting Tomorrow, 26 Mar 2014 15:18, if it is tomorrow's date and Wednesday, 27 Mar 2014 15:18, if it is day after tomorrow's date.
Anyone please help me.
Sample from developer.apple.com
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:frLocale];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*1];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
NSDateFormatter *detailFormatter = [[NSDateFormatter alloc] init];
[detailFormatter setLocale:frLocale];
[detailFormatter setDateFormat:#"dd MMM yyyy HH:mm"];
NSString *detailString = [detailFormatter stringFromDate:date];
NSLog(#"detailString: %#", detailString);
NSString *finalString = [NSString stringWithFormat:#"%#, %#",dateString, detailString];
NSLog(finalString);
Apple doesn't directly support using relative date combined with absolute date. What you'd have to do is to create two formatters, one with relative enabled and one without. Then compare the output strings (e.g. "Today" and "Mar 25") and if they are not equal, combine them: "Today, Mar 25", but if they are equal, just use one.
And as an aside, actually on "après-après-demain", I think you may have found a bug (either in docs or code). Do you actually set "après-après-demain"? I tried Apple's sample code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"fr_FR"];
[dateFormatter setLocale:frLocale];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
Which Apple says produces output of dateString: après-après-demain no matter what your language is set to (as it switches the date formatter to French).
But when I ran the code on both iOS and OS X, I get the actual date: Mar 28 (and if I change to *2, I get après-demain.
So, if you're seeing the same, I'd suggest filing a bug report with Apple.
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
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]];
I use this code to process a date string coming in from a json feed:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle: NSDateFormatterLongStyle];
[formatter setFormatterBehavior: NSDateFormatterBehavior10_4];
[formatter setDateFormat: #"EEE, dd MMM yyyy HH:mm:ss +0000"];
so if I call
NSDate *date = [formatter dateFromString: #"Tue, 08 Sep 2009 19:21:27 +0000"];
I get back a usable date if my region format is United States or United Kingdoms, but if I set it to Germany it returns nil. I understand there are some differences in behaviors across different locales, but if I define a format shouldn't that correct for any inconsistencies?
Names like "Tue" and "Sep" are English. Other languages use different names.
If you want to be able to parse English dates independent of the device's region settings, set your DateFormatter's locale to en_US using the -setLocale: method.
Thanks fixed it up with:
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"US"] autorelease]];
I need the date as a string but not the time and it has to be localized.
So for example USA should be Sep 25 2009 but for New Zealand it would be 25 Sep 2009.
I can get the date into a string by specifying the format "MMM dd YYYY" but It's not localized.
Any ideas?
[NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterMediumStyle
timeStyle:0]
The key is to send setTimeStyle:kCFDateFormatterNoStyle to the dateFormatter. That sets the dateFormatter so that it will only format the date and not output the time:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale: [NSLocale currentLocale]];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle]; // or whichever style...
[dateFormatter setTimeStyle:kCFDateFormatterNoStyle];
NSString* dateString = [dateFormatter stringFromDate: [NSDate date]];
This gives me the following output:
12/18/09