Comparing dates in Cocoa

不想你离开。 提交于 2019-12-04 02:18:14

问题


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.


回答1:


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.




回答2:


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
}


来源:https://stackoverflow.com/questions/1811779/comparing-dates-in-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!