开发者

Converting NSStrings in an array into NSDates

开发者 https://www.devze.com 2023-03-02 19:11 出处:网络
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 consis

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, dat开发者_JAVA技巧e 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"];
0

精彩评论

暂无评论...
验证码 换一张
取 消