Converting can NSString to NSDate format in objective C [duplicate] - iphone

This question already has answers here:
Convert string to date in my iPhone app
(4 answers)
issue with date formatting using NSDateFormatter
(3 answers)
Closed 9 years ago.
How can i convert the following string "October 24,Monday 12:30 AM EST" to NSDateFormat.
i tried this code
NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
[dateformat setDateFormat:#"MM/DD/YYYY hh:mm a"];
NSDate *datefor=[dateformat dateFromString:appDelegate.appoinmentString];
[dateformat setDateFormat:#"MM-dd-yyyy hh:mm a"];
NSString *dateStr=[dateformat stringFromDate:datefor];
NSDate *datetype=[dateformat dateFromString:dateStr];

Your dateformat for October 24,Monday 12:30 AM EST is not correct. The correct dateformat in your case is MMMM dd,eeee HH:mm a z.
Working Code :
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"MMMM dd,eeee HH:mm a z"];
NSDate *myDate = [dateformat dateFromString:#"October 24,Monday 12:30 AM EST"];
Take a look at Date Format Specifiers.
eeee - Local day of week spelled out.
MMMM - Month spelled out.
dd - day of month with no leading zeros.
HH - hour of day (24 hour format).
mm - minutes of hour (with leading zero) .
z - timezone (short wall
time).

Try this...
NSString *p=#"October 24,Monday 12:30 AM EST";
NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
[dateformat setDateFormat:#"MMM dd,EEEE hh:mm a vvv"];
NSDate *datefor=[dateformat dateFromString:p];
[dateformat setDateFormat:#"MM-dd-yyyy hh:mm a"];
NSString *dateStr=[dateformat stringFromDate:datefor];
NSDate *datetype=[dateformat dateFromString:dateStr];
Let me know if you have any problem.

Try like below:-
NSString *dateStr=#"October 24,Monday 12:30 AM EST";
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateFormat setDateFormat:#"MMMM dd,eeee HH:mm a z"];
NSDate *changeDate=[dateFormat dateFromString:dateStr];
NSLog(#"changeDate=%#",changeDate);
NSString *str=[dateFormat stringFromDate:changeDate];
//getting your local time
NSTimeZone *tz=[NSTimeZone localTimeZone];
//setting yourlocal time
[dateFormat setTimeZone:tz];
NSDate *date = [dateFormat dateFromString:str];
//Setting your desired format
[dateFormat setDateFormat:#"dd-mm Z"];
NSString *newDate=[dateFormat stringFromDate:date];
NSLog(#"new date=%#",newDate);

Related

NSDateFormatter formatting issue

I am trying to convert NSString into NSDate in 12 hours format. (in iOS 6)
Code :
NSString *Bdt = #"05/23/2012 08:00 AM"
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSLog(#"%#",bd);
Output:
2012-05-23 08:00:00 +0000 it should be 2012-05-23 08:00 AM
Whats wrong in the code ?
Thanks
If you want date in 2012-05-23 08:00 AM style
Create a dateformatter and setDateFormat as yyyy-MM-dd hh:mm a
NSString *Bdt = #"05/23/2012 08:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init] ;
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *bd = [df dateFromString:Bdt];
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a" ];
NSString *datestr=[dateFormatter stringFromDate:bd];
NSLog(#"%#",datestr);
*Note : I am using ARC, so objects are not released/autoreleased.
EDIT:
NSDate will be in this format ONLY : 2012-05-23 08:00:00 +0000.
For any other format you need to use NSString.
Nothing is wrong with it. You are printing an NSDate in NSLog which is very different than creating an NSString with a specific format.
It seems your confusing the internal NSDate representation with string formatting. NSDate stores the date internally in a way different from how it is represented by humans. Just like NSString stores strings in a format that may not be what you ultimately want it encoded as, eg. ASCII or UTF-8. When you are calling NSLog you are getting a diagnostic message showing the date according to the string returned by - (NSString *)description or possibly - (NSString *)debugDescription.

Convert string to date(0512 should be converted to May 2012) [duplicate]

This question already has answers here:
Convert string to date in my iPhone app
(4 answers)
Closed 10 years ago.
How to convert a string to date? I know we can use dateFormatter to do this but I am stuck in between. I have format 0512 which should be converted to May 2012.
Try this,
NSString *finalDate = #"0512";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMyy"];
NSDate *date = [dateFormatter dateFromString:finalDate];
[dateFormatter setDateFormat:#"MMM yyyy"];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
Try this
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMyy"];
NSDate *dateObj= [dateFormatter dateFromString:#"0512"];
NSLog(#"%#",dateObj);
[dateFormatter setDateFormat:#"EEEE MMMM d, YYYY"];
NSLog(#"%#",[dateFormatter stringFromDate:dateObj]);
Try this code may be helped you..
NSString *dateStr = #"0512";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMyy"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"MMMM YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"%#",dateStr);
What is so difficult about it?
1. Take out first 2 characters of string
2. Put if else/ convert string to int and use switch to make up month
3. Take the next 2 characters, add them to 2000 and you get year
Now user iOS functions to achieve this :)

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.

NSDateFormatter in iPhone app? [duplicate]

This question already has answers here:
Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds
(8 answers)
Closed 9 years ago.
Am receiving date in this format (2012-07-13 09:22:22 +0000). I want to convert this date format to 13-Jul-2012 09:22. And also if the date is today means i want to change Today 09:22 and Yesterday 09:22. Can anyone please help to do this?
EDIT:
I have added some code what i have tried.
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"dd-mm-yyyy hh:mm"];
[dateFormatter1 setFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormatter1 dateFromString:dateStr];// Change Date Format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDoesRelativeDateFormatting:YES];
NSString *dateString1 = [formatter stringFromDate:date]; // Getting Today or Tomorrow
[formatter release];
NSLog(#"DateString1 : %#", dateString1);
First problem I see is that the date pattern your supplying to the Formatter does not match the date string you claim your getting. You might want to try this:
[dateFormatter1 setDateFormat:#"yyyy-mm-dd hh:mm:ss Z"];
The format your using in the code you supplied has year, month, day reversed and your not converting second.
You can try this:
NSDate *date = [NSDate date]; // date in this format 2012-07-14 17:32:09 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm"];
//[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; // Optional
NSString *dateStr = [dateFormatter stringFromDate:date];
And next compare your 'date' with current date using NSDate methods for eg. timeIntervalSinceReferenceDate and then based on the result display today/tomorrow.

NSDateFormatter not working properly?

My code ,
-(NSString *)trimDateStringInRequiredFormat:(NSString *)dateInString{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateInString];
[dateFormatter setDateFormat:#"hh:mm aaa"];
NSString *dateDisplay = [dateFormatter stringFromDate:date];
return dateDisplay;
}
The above worked properly when my date is 2012-06-06 12:00:00 and I got appropriate output like 12:00 AM. But when I send my date like 2012-06-06 15:00:00 it doesn't return me appropriate output, instead, it returns me null output. When I'm trying to trace this function the 2012-06-06 15:00:00 date on NSDate *date = [dateFormatter dateFromString:dateInString]; this line not properly converted. Why did this happen? where am I wrong in this code???
try doing:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
With hh you have the 12h format, with HH you use 24h format
This is because for 24 Hr format you need to use HH instead of hh