Compare the number of different days between two dates, such as the number of months and the number of days

Source: Internet
Author: User

Compare the number of different days between two dates, such as the number of months and the number of days

// Compare the selected Start Time and End Time

+ (BOOL) ComparisonString :( NSString *) str1 andString :( NSString *) str2

{

// NSString * dateStr = @ "01:01:01"; // input time

// 1. Obtain the time

NSDate * senddate = [NSDate date];

NSDateFormatter * dateformatter = [[NSDateFormatter alloc] init];

[Dateformatter setDateFormat: @ "yyyy-MM-dd"];

NSString * locationString = [dateformatter stringFromDate: senddate];

Str2 = locationString;

NSArray * arrya1 = [str1 componentsSeparatedByString: @ ""];

Str1 = arrya1.firstObject;

Arrya1 = [str2 componentsSeparatedByString: @ ""];

Str2 = arrya1.firstObject;

NSString * dateStr = [NSString stringWithFormat: @ "%@ 00:00:01", str1];

// Input time

// Convert the input time to the required format

NSDateFormatter * format = [[NSDateFormatter alloc] init];

[Format setDateFormat: @ "yyyy-MM-dd HH: mm: ss"];

NSDate * fromdate = [format dateFromString: dateStr];

NSTimeZone * fromzone = [NSTimeZone systemTimeZone];

NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];

NSDate * fromDate = [fromdate dateByAddingTimeInterval: frominterval];

NSLog (@ "call time = % @", fromDate );

// NSString * dateStr1 = @ "20:28:40"; // input time

NSString * dateStr1 = [NSString stringWithFormat: @ "%@ 23:59:59", str2];

// Input time

// Convert the input time to the required format

NSDateFormatter * format1 = [[NSDateFormatter alloc] init];

[Format1 setDateFormat: @ "yyyy-MM-dd HH: mm: ss"];

NSDate * fromdate1 = [format1 dateFromString: dateStr1];

NSTimeZone * fromzone1 = [NSTimeZone systemTimeZone];

NSInteger frominterval1 = [fromzone1 secondsFromGMTForDate: fromdate1];

NSDate * fromDate1 = [fromdate1 dateByAddingTimeInterval: frominterval1];

NSLog (@ "call time 1 = % @", fromDate1 );

Double intervalTime = [fromDate1 timeIntervalSinceReferenceDate]-[fromDate timeIntervalSinceReferenceDate];

Long lTime = (long) intervalTime;

NSInteger iDays = lTime/60/60/24;

// NSInteger iMonth = lTime/60/60/24/12;

// NSInteger iYears = lTime/60/60/24/384;

// NSInteger iSeconds = lTime % 60;

// NSInteger iMinutes = (lTime/60) % 60;

// NSLog (@ "dat = % d", iDays );

If (iDays <= 0 ){

Return NO;

} Else {

Return YES;

}

}

 

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.