views:

441

answers:

2

hi i want to know how to get current date? i want to compare current date with a date i m fetching from a plist files using following code.

NSDate *expDate = [licenseDictionary objectForKey:@"Expires"];

for comparison i m using the following code

if([curDate compare:expDate] == NSOrderedAscending )

but its not working. can anybody help me.

+2  A: 

To get the current date, simply use:

NSDate * today = [NSDate date];

To compare to another date:

if ([today compare:expirationDate] == NSOrderedAscending)
{
    // today's date is before the expiration date
}
e.James
thanks james. i have tried this one but still not working in my case. for expiration date, what should be the date format?
Shakti
Are you storing an NSDate in your dictionary or a string representation of the date?
Abizern
I am creating a license file in the form of a plist. In that plist, under "Expires" key, i have add the date like November 26, 2009 as a value for that key. In the validation code i have assigned that value to NSDate object as NSDate *expDate = [licenseDictionary objectForKey:@"Expires"];using this value of expDate i m comparing the current date. No error is coming but still i m not getting the desired result
Shakti
+4  A: 

In the interest of teaching someone how to fish rather than just feeding him:

Have a look at the Date and Time Programming Guide.. The Programming Guides in the documentation are your first stop when trying to understand a topic. They provide an overview of what can be done and contain useful example code.

These guides also have links to the documentation of the specific classes that are used. In this case there is the NSDate Class Reference which has sections on creating dates and comparing dates.

Edit

To answer you comment about this not working, I think the problem could be that you haven't created the object that you've stored in the dictionary as an NSDate. Again. Have a look at the creating dates documentation. It will be something like

NSDate *expiryDate = [NSDate dateWithNaturalLanguageString:@"31/01/10"];

But this is just an example, there are other ways of setting a date string.

Abizern
You can store a date object in a property list, as long as you're not using the OpenStep format. So the questioner doesn't need to reconstitute it from a string, because they shouldn't put it into the plist as a string in the first place.
Peter Hosey
Sorry, that's what I meant. The OP should be storing the date object in the dictionary directly, I was just trying to give an example of how he could create this date since the question is asking how to create a date.
Abizern
I am creating a license file in the form of a plist. In that plist, under "Expires" key, i have add the date like November 26, 2009 as a value for that key. In the validation code i have assigned that value to NSDate object as NSDate *expDate = [licenseDictionary objectForKey:@"Expires"]; using this value of expDate i m comparing the current date. No error is coming but still i m not getting the desired result
Shakti
Have you stored the expiry date in the plist file as a proper NSDate object?
Abizern
i am storing it like November 26, 2009. is this of type NSDate object?
Shakti
No. Just storing it as a string doesn't make it an NSDate object. You need to create an NSDate object as I've shown in my answer, or by using other NSDate creation methods in the documentation.
Abizern