Comparing dates in Cocoa

前端 未结 2 967
走了就别回头了
走了就别回头了 2021-01-16 06:10

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 =         


        
相关标签:
2条回答
  • 2021-01-16 06:47

    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
    }
    
    0 讨论(0)
  • 2021-01-16 06:51

    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.

    0 讨论(0)
提交回复
热议问题