Compare the size of two dates in iOS programming

Source: Internet
Author: User
Tags date1 time zones
<span id="Label3"></p><p><p>Comparing date sizes is a common problem in any programming language, and in iOS programming, you typically use NSDate objects to store a time (including dates and times, time zones), and the NSDate class provides a compare method for time Comparisons. But sometimes you don't want to know the size of two dates so precisely (by default it compares to seconds), you can use the following implementation:</p></p><pre><pre>+ (int) compareoneday: (nsdate *) oneday withanotherday: (nsdate *) anotherday{ nsdateformatter *dateformatter = [[ NSDateFormatter alloc] init]; [dateformatter setdateformat:@ "dd-mm-yyyy"]; NSString *onedaystr = [dateformatter stringfromdate:oneday]; NSString *anotherdaystr = [dateformatter stringfromdate:anotherday]; NSDate *datea = [dateformatter datefromstring:onedaystr]; NSDate *dateb = [dateformatter datefromstring:anotherdaystr]; Nscomparisonresult result = [datea compare:dateb]; NSLog (@ "date1:%@, date2:%@", oneday, anotherday); if (result = = Nsordereddescending) { //nslog (@ "Date1 is in the future"); return 1; } else if (result = = Nsorderedascending) { //nslog (@ "Date1 is in the past"); return-1; } NSLog (@ "Both Dates is the same"); return 0; }</pre></pre><p><p>Compare the size of two dates in iOS programming</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.