NSDate in format dd/MM/yyyy [duplicate] - iphone

This question already has an answer here:
Convert NSDate to mm/dd/yyyy
(1 answer)
Closed 9 years ago.
I want NSDate in dd/MM/yyyy format, without its time i.e. hh:mm:ss format. I can get string in this format but not nsdate. How to get nsdate like 27/12/2013?
Thanks in advance!

You can use NSDateFormatter to format NSDate variables. In your case you would use it like this:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yyyy";
NSLog(#"my date is %#",[dateFormatter stringFromDate:yourNSDateVariable]);

NSFormatter does this very well and easily:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSString *dateInString = [formatter stringFromDate:yourdate];

You can not "format" an NSDate, but instead only a string representation of it (which you say that you already know how to do). Take a look at the NSDate class reference for more details: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/

NSDateFormatter *oldFormatter = [[NSDateFormatter alloc] init];
[oldFormatter setDateFormat: #"dd/MM/yyyy HH:mm:ss"];
NSString *oldString = #"27/12/2013 21:30:21";
NSDate *theDate= [oldFormatter dateFromString:oldString];
NSDateFormatter *newFormatter = [[NSDateFormatter alloc] init];
[newFormatter setDateFormat: #"dd/MM/yyyy"];
NSString *yourString = [newFormatter stringFromDate:theDate];
NSLog(#"%#", yourString);

Related

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

Getting date in specified format [duplicate]

This question already has answers here:
Passing a formatted NSDate
(3 answers)
Closed 8 years ago.
I am getting current date in format "2013-02-10 09:26:52 +0000" by using [NSDate date].
I want date in format like "Feb 10".
Can anyone suggest correct dateFormat?
Use NSDateFormatter class:
NSDateFormatter * myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:#"ddMMyy" options:0 locale:[NSLocale currentLocale]];
NSString * formattedDate = [myFormatter stringFromDate:[NSDate date]];
Here you have info to choose the output format for setDateFormat: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
Hi set the date formatter like this
[dateFormatter setDateFormat:#"MMM d, yyyy hh:mm:ss"];
hope it will work
It is working in my case :
NSDateFormatter* dateFormatterBegin = [[NSDateFormatter alloc] init];
[dateFormatterBegin setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
NSString *dateStringBegin = #"2013-02-10 09:26:52 +0000";
NSDate* datebegin = [dateFormatterBegin dateFromString:[dateStringBegin stringByReplacingOccurrencesOfString:#":" withString:#"" options:0 range:NSMakeRange(dateStringBegin.length-3, 1)]];
[dateFormatterBegin setDateFormat:#"MMM-dd"];
NSString *beginDate = [[NSString alloc] initWithFormat:#"%s%#","Start Date : ",[dateFormatterBegin stringFromDate:datebegin]];
NSLog(#"Your date is : %#",beginDate);
[dateFormatterBegin release];
[beginDate release];
NSString *string = #"2013-02-10 09:26:52 +0000"; // The date in a string
//Convert the string to date with the same format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"]; // Set format
NSDate *date = [dateFormat dateFromString:string];
//Create the new date format using NSDateFormatter
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MMM dd"]; // Set format
//Convert the date using dateFormat1
NSDate *formatDate= [dateFormat1 dateFromString:date]; // date with MMM dd

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.

How to convert Date like "2012-12-26" to "december 26, 2012" in iOS ?

I want to convert date 2012-12-26 to december 26, 2012 in iOS?
I am using websrvice and the data comes in this format 1990-12-26.
I want to change this to december 26, 2012 format.
This is what I am doing:
lbl_Rightside.text = [rootElement stringValueForNode:#"date"];
NSLog(#"lbl_Rightside is %#",lbl_Rightside.text);
[lbl_Rightside release];
Getting date to this label on 1990-12-26. Now I want to change date to december 26, 2012 format.
Any hints from experts would be very welcome.
you can use NSDateFormatter to do this kind of things. First
convert your date String to a date object using dateFromString:
method.
from date convert to string you want using stringFromDate: method
Different format strings can be found here.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *orignalDate = [dateFormatter dateFromString:YOUR_ORIGINAL_STRING];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *finalString = [dateFormatter stringFromDate:orignalDate];
[dateFormatter release]; //if not using ARC
Check the official Apple documentation about NSDateFormatter. You should use this class to do this kind of formatting.
by using NSDateFormatter
NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Try to look at NSDateFormatter Class,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; // this is your input date format
NSDate *newDate = [dateFormatter dateFromString:dateString];//converting string to date object
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy"]; // setting new format
NSLog(#"The date is = %#",[dateFormatter stringFromDate:newDate])

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"];