2

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

一二三
  • 21,059
  • 11
  • 65
  • 74
Joe
  • 145
  • 1
  • 10

4 Answers4

4

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");
}
Gypsa
  • 11,230
  • 6
  • 44
  • 82
1
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

Alan Zeino
  • 4,406
  • 2
  • 23
  • 30
Tendulkar
  • 5,550
  • 2
  • 27
  • 53
1

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

Community
  • 1
  • 1
yan.kun
  • 6,820
  • 2
  • 29
  • 38
0

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"];
Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52