Reputation: 1723
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);
Upvotes: 0
Views: 462
Reputation: 90117
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.
Upvotes: 0
Reputation: 2399
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
Upvotes: 1
Reputation: 1227
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];
Upvotes: 1
Reputation: 3738
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.
Upvotes: 0
Reputation: 47
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
Upvotes: 1
Reputation: 2807
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.
Upvotes: 1