How can I get the date but not time from NSDate - iphone

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

Related

How to change the date type to "Jan 21, 2013 5:30 AM" using NSDateFormatter in iOS

I wish to change the date format from "2013-01-21 11:55:00" to "Jan 21, 2013 5:30 AM".
I tried the below code. But it gives a constant date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSDate *myDate = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
NSLog(#"%#",[dateFormatter stringFromDate:myDate]);
The above code gives constant output : Jan 3, 2001 2:30:00 AM
But in my case, date is dynamic.
NSString *firstDate = #"2013-01-21 11:55:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd HH':'mm':'ss"];
NSDate *date = [dateFormatter dateFromString:firstDate];
[dateFormatter setDateFormat:#"MMM d, yyyy h:mm a"];
NSLog(#"Expected Result___ %#",[dateFormatter stringFromDate:date]);
Try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy h:mm a"];
NSDate *myDate = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
NSLog(#"%#",[dateFormatter stringFromDate:myDate];
Try [NSDate date] instead [NSDate dateWithTimeIntervalSinceReferenceDate:162000].
well you get constant output since you give constant input as 162000 !!!
First get the date from the string using its format then use the new format to get string from the obtained date
use this to output format as
#"MMM d, yyyy h:mm a"
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMM d, yyyy h:mm a"];
NSLog(#"%#",[dateFormatter stringFromDate:[NSDate date]]);

iOS - Converting string to non-en-GB locale NSDate returns nil [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSString to NSDate
I have a date in format Mon Jan 14 14:00:00 CET 2013 I try to convert it to NSDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
but it doesn't work and my date is nil
Input data is in en-GB locale, my device's locale is nb-NO
Any suggestions?
You're missing day in your format:
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
If it was not a typo, then next thing is to set proper locale so formatter will recognise CET timezone, for example en-GB will fix that:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
// that will fix the problem with not recognized CET timezone
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en-GB"]];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
Try to use this function
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss a"];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
I hope this will helps u.
I think your Time Zone is wrong. Just use this code , it will work Perfectly :
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 EDT 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date :: %#",date);
It will log Output as :
date :: 2013-01-14 18:00:00 +0000
EDIT :
I found Something for you : NSDateFormatter doesn't parse some timezones
You can solve this by using en_GB Locale , as stated : "These abbreviations do still work with the en_GB locale" in Working with Date and Time in Cocoa .
CET is not recognised
Try this :-
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"%#",date);
Hope it helps you
Simply "CET" is not a recognized time zone by NSDateFormatter.
Also the date/tine is over specified, best to not try include the day or week (Mon).
Here is an example that demonstrates working code with a recognized timezone:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 EST 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date: %#", date);
NSLog output
date: 2013-01-14 19:00:00 +0000
NSLog(#"abbreviationDictionary: %#", [NSTimeZone abbreviationDictionary]);
does show
CET = "Europe/Paris";
so this looks like an Apple bug in NSDateFormatter.
Report the bug at: Apple Bug Reporter
You can use:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
If you still want to custom your date format try this one:
yyyy-MM-dd HH:mm:ss ZZZ
Because can't invent your own formatted string syntax and expect it to work; you need to actually use a documented format as the documentation points it out : Formatters in OS X v10.8 and iOS 6.0 use version tr35-25.
-> https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
If you are curious: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

NSDate from NSString in iPhone

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.

NSString to NSDate keeps getting null

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);

NSDateFormatter Does not convert correctly

I have a NSString which is passed from an xml feed........
NSString *strDate =#"Thu, 22 Apr 2010 10.30 am CEST";
I'm currently using this code to format the date........
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm a vvvv"];
NSDate *myDate = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:#"HH"];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
I want to format my string only to display only hours and currently I'm getting value like this.
strDate =#"2010-04-10 14:00:00 +0530";
Can anyone please help me with this?......
I'm sorry.It's my mistake.It should be like this.
NSString *strDate =#"Thu, 22 Apr 2010 10:30 am CEST";
What my requirement is to get hour part only from above string using NSDateFormatter. How can I achieve that. Sorry for the earlier mistake.
Thanks.
If you want to get the 10 of 10:30 (if its ur requirement) then you can do it like:
strDate = #"Thu, 22 Apr 2010 10:30 am CEST";
NSArray *dateComponents = [strDate componentsSeparatedByString:#" "];
NSString *requiredString = [dateComponents objectAtIndex:4];
dateComponents = [requiredString componentsSeparatedByString:#":"];
requiredString = [dateComponents objectAtIndex:0];
and when you do:
NSLog(rquiredString);
Output : 10;
This is just a workaround, for better approach you should go through the NSDateComponents class.
Hope this helps.
Thanks,
Madhup
change to
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh.mm a vvvv"];
(. instead of : in hh:mm)
You want to do this:
NSString *strDate =#"Thu, 22 Apr 2010 10:30 am CEST";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm a"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:#"hh"];
strDate = [dateFormatter stringFromDate:myDate];
NSLog(#"%#", strDate);
(Firstly your original formatter was wrong, you had a: instead of a .) EDIT no longer the case
Secondly, you want to ignore the CEST bit as this will cause your timezone being changed