Convert NSDate to mm/dd/yyyy - iphone

I know this question is similar to others asked, but I can't find where my code is going wrong. I am trying to convert the current date into a mm/dd/yyyy format. Here is what I am using:
NSDate *date = [[NSDate alloc]init];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"mm/dd/yyyy"];
NSLog(#"%#", [formatter stringFromDate:date]);
The problem is the month. Every time I run this code, the month is different. For example the first time my date was 32/11/2012, and I just ran it and it was 55/11/2012.
Is there a reason why this would keep changing? The days and years appear to be fine.
Thanks.

Check the case:
mm means minutes
MM means months
So your code should probably read
NSDate *date = [[NSDate alloc]init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
NSLog(#"%#", [formatter stringFromDate:date]);

Related

How to show only week and time in DatePickerView?

In my application, i want to raise the notification using selected day and time, not with date.So i have to show the day and time only in DatePickerView. Would you please help me in this problem. Thanks in advance.
You should use NSDateFormatter for this purpose.
For example:
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// e.g. "Tue 16:53"
[dateFormatter setDateFormat:#"EEEE HH:mm"];
NSString *szDate = [dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"Date formatted: %#", szDate);
(remove release if ARC is enabled);
And btw, the questions of this kind were asked million times around stackoverflow

NSDateFormatter not working while formatting [duplicate]

This question already has an answer here:
dateformatter problem?
(1 answer)
Closed 8 years ago.
I have a time string, which is as shown below:
2/5/2013 12:00:00 AM
and i want to change this in to MM/dd/yyyy formate.
i used NSDateFormatter, but i am getting a null value. how can i change this??
check your code, must be doing something wrong, this is how you receive the right format..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [formatter dateFromString:str];
Read this document for date formatters.

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.

Converting NSStrings in an array into NSDates

I'm working on an assignment that allows the user to display events that are happening 'today'. I have parsed the XML file and stored the contents into an array. The contents of the XML file consists of a title, description, date etc. The dates are in NSString format and I want to convert them into NSDates and compare them with today's date before displaying them in a UITableView.
I'm new to obj-c and I've searched online for help on NSDate, but I couldn't find what I need. Any links, advice or help on this is really appreciated. Thanks in advance (:
suppose dateString contains the date in string format
first get date from string:-
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/mm/yyyy"];
NSDate *dateprevious = [formatter dateFromString:dateString];
Now get today date
NSDate *date=[NSDate date];
[formatter setDateFormat:#"dd"];
NSString *dateOfGame =[formatter stringFromDate:dateprevious];
NSString *todaydate =[formatter stringFromDate:date];
[formatter release];
if([todaydate isEqualToString:dateknown])
{
NSLog(#"date matched");
}
Depending on the format of the string, you can use this:
+ (id)dateWithNaturalLanguageString:(NSString *)string
To compare two dates you will find here a lot of usefull answers :)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy , hh:mm a"];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [[dateFormatter datefromString:date] retain];
[dateFormatter release];
You can use this one
Have a look at NSDateFormatter
It has a method called dateFromString
Like you could do the following:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/mm/yyyy"];
NSDate *date = [formatter dateFromString:#"5/5/2011"];

Formatting a date

Edit: Updating post based on Martins comments below
Hello,
I'm just trying to get a date from a string, my code is as follows:
NSString *strStartDateTime = [startTimeArray objectAtIndex:i];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dtStartDateTime;
dtStartDateTime = [formatter dateFromString: strStartDateTime];
strStartDateTime has a value of '2010-09-30 15:23:30', but this code returns a value of '2010-09-30 14:23:30 GMT', all I want is '2010-09-30 15:23:30' as per the string.
Is it taking Day light Savings Time into consideration. This is very frustrating, I simply want to convert the NSString to NSDate and its changing the value.
Regards,
Stephen
I don't quite understand your question. What do you mean by "it gives me the GMT value"? What is your input? What is your expected output?
If you have problems with time zone conversions, you could try to set the time zone for the formatter:
[formatter setTimeZone:[NSTimeZone localTimeZone]];
or
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
As I said, it all depends on what the input and expected output is.
EDIT:
You actually don't have a problem. The date is parsed correctly. You can verify it this way:
NSString *strStartDateTime = [startTimeArray objectAtIndex:i];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dtStartDateTime = [formatter dateFromString: strStartDateTime];
NSString *parsedDateAsString = [formatter stringFromDate:dtSTartTime];
// parsedDateAsString will have the correct value