Comparison of iOS dates

Source: Internet
Author: User
Tags date1

Comparison of iOS dates

1. dates can be compared to determine the size or equality, or to determine the time interval between two dates. You can use the-timeIntervalSinceDate method to calculate the time difference between two dates.

  1. NSDate * now = [NSDate date];
  2. NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
  3. NSTimeInterVal timeBetween = [now timeIntervalSinceDate: anHourAgo];
  4. NSLog (@ "% f", timeBetween );
    2. You can use the-timeIntervalSinceNow method to obtain the date comparison and the current time interval.
      
    1. NSDate * anHourago = [NSDate dateWithTimeIntervalSinceNow;-60*60];
    2. NSTimeInterval interval = [anHourAgo timeIntervalSinceNow];
    3. NSLog (@ "% f", interval );

      // Create Date Format object NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; // Two date objects are created. NSDate *date1=[dateFormatter dateFromString:@"2010-3-3 11:00"]; NSDate *date2=[dateFormatter dateFromString:@"2010-3-4 12:00"]; //NSDate *date=[NSDate date]; //NSString *curdate=[dateFormatter stringFromDate:date]; // Take the time interval of two date objects: // Here NSTimeInterval is not an object, it is a basic type, in fact, it is a double type, which is defined by c: typedef double NSTimeInterval; NSTimeIntervaltime=[date2 timeIntervalSinceDate:date1]; int days=((int)time)/(3600*24); int hours=((int)time)%(3600*24)/3600; NSString *dateContent=[[NSString alloc] initWithFormat:@"% I day % I hour",days,hours];

      3. NSDate also provides the-laterDate,-earlierDate, and compare methods to compare dates.
        
      1. NSDate * now = [NSDate date];
      2. NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
      3. NSDate * result1 = [now laterDate: anHourAgo];
      4. NSDate * result2 = [now earlierDate: anHourAgo];
      5. NSComparisonResult result3 = [now compare: anHourAgo];

        Comparing the date size is a common problem in any programming language. In iOS programming, the NSDate object is usually used to store a time (including the date, time, and time zone ), in addition, the NSDate class provides the compare method to compare the time, but sometimes you do not want to know the size of the two dates so accurately (the default value is the second). You can use the following implementation method:

        +(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 are the same");    return 0;             }

Related Article

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.