How can I convert string into date? - iphone

I have two strings:
date1 = 3-5-2014;
date2 = 4-2-2010;
I have to convert them in date and then compare them. I want the same format of date as in strings i.e., dd-mm-yyyy. How it can be done?

NSString *string=#"03-05-2014";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:string];
enjoy...

You can extract NSDate objects from strings using the NSDateFormatter class. See the Date Formatters section Apple's Data Formatting Guide for a detailed explanation and sample code.

Try with below
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
here is the SO post
Convert NSString date to NSDate

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormatter dateFromString:#"25-8-2010"];
NSLog(#"%#", date);
[dateFormatter release];
Hope this works for you
Cheers :)

Related

NSDateformatter does not format correctly

I have an NSDateformatter that is not working correctly. I got my date as a string like this.
2013-02-20 00:00:00
And I need to return it as an NSDate like this
2013-02-20
What I've got at the moment is this.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat dateFromString:dateString]
But this is not working.
Any help ?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate* date=[dateFormat dateFromString: dateString];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#",[dateFormat stringFromDate: date]);
This will work
try it
if you have more than on date to parse, it could be worthy to create one input and one output date formatter
static NSDateFormatter *inputDateFormatter;
static NSDateFormatter *outputDateFormatter;
if (!inputDateFormatter) {
inputDateFormatter= [[NSDateFormatter alloc] init];
[inputDateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:#"yyyy-MM-dd"];
}
NSDate *date = [inputDateFormatter dateFromString:#"2013-02-20 00:00:00"];
NSString *string = [outputDateFormatter stringFromDate:date];
NSLog(#"%#", string);
result
2013-02-20
as NSDateFormatter are expensive to create, you should either store them in a singleton or declare them static to create them once for that method.
And I need to return it as an NSDate like this
A date has no format, it is just a point in time. you will have to creta a string with the desired format where needed.

Objective-C String(yyyy-mm-dd) to NSDate

My App is a JSON code which involves a date in string type. (Such as "2011-10-01").
I would like to know how I could conver it into NSDate?
It needs to be displayed in a different time format such as "1 October, 2011".
ex. this code doesn't work:
NSString *date1 = #"2010-11-12";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MMMM-dd"];
NSDate *date2 = [dateFormat dateFromString:date1];
NSString *strdate = [dateFormat stringFromDate:date2];
As Tug writes in his blog post on the subject:
Objective-C and iOS SDK provide a class to help formatting date
(marshaling and unmarshaling), this class is NSDateFormatter. No
surprise, the NSDateFormatter uses the Unicode Date Format
Patterns.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:publicationDate ];
[dateFormatter release];
where publicationDate in this case is an NSString.
Use NSDateFormatter. The appropriate method is dateFromString:. Take a look at the documentation :)
You can try this
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"dd MM yyyy"];
NSString *todayString = [df stringFromDate:[NSDate date]];
NSDate *someDate=[NSDate date];
NSString *targetDateString = [df stringFromDate:someDate];
NSLog(#"date:%#",targetDateString);

NSDate and NSString cocoa

For my iPhone app I got to convert NSDate object to string, and then convert the string back to NSDate object.. Can someone help me out? thank you!
Use to convert from NSDate to NSString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
Use to convert from NSString to NSDate.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString: stringFromDate];
You need to check out NSDateFormatter this does exactly this, both directions.
convert NSDate to NSString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
Convert NSString to NSDate
NSString *dateString = #"01-02-2010";
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:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

How to specify the NSDate Format in Iphone app?

I am trying to make a date format in such a manner hours.minutes day/month/year
I have uses this code :
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
NSDate *dateAndtime = [NSDate date];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
//[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString* dateforBD=[[NSString alloc]init];
dateforBD =[formatter stringFromDate:dateAndtime];
But this code not give me my format.
Also i have tried this code:
NSLocale *locale = [NSLocale currentLocale];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:#"E MMM d yyyy"
options:0 locale:locale];
[formatter setDateFormat:dateFormat];
[formatter setLocale:locale];
It's also give me different format. How can i achieve the required format means 13.10 21/4/2011
All the formats return in month before date. May i have used wrong way for this ?
Thanks in advance.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];
To explore more visit http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/
Hope it helps.
for date before month use -
[dateFormat setDateFormat:#"dd/MM/yyyy hh:mm"];
As specified in Apple's NSDateFormatter reference, the NSDateFormatter uses Unicode Date Format Patterns.
According to the unicode patterns, you would need the following pattern to achieve "13.10 21/4/2011":
hh:mm dd/MM/yyyy
So you would use this code:
NSDate *dateAndtime = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm dd/MM/yyyy"];
NSString *newlyFormattedDateString = [dateFormatter stringFromDate:dateAndtime];

Formatting a Date String using NSDateFormatter

I have a NSString which is passed from an xml feed........
NSString *strDate =#"May 14 2010";
I'm currently using this code to format the date........
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"d:MMM"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
I want to format my string only to display day and month and currently I'm getting a nil value to the NSDate. Can anyone please help me with this?......
Thanks.
I think you're slightly misunderstanding how NSDateFormatter works. It cannot automatically work out what format the date in your string is in.
If you change your code to something like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd yyyy"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:#"d:MMM"];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
It should do what you want.