The time interval for calculating two dates in iOS development, and the time interval for calculating two dates in ios development

Source: Internet
Author: User
Tags date1

The time interval for calculating two dates in iOS development, and the time interval for calculating two dates in ios development

// Create a formatted object

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

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

// Create a date object

NSDate * date1 = [dateFormatter dateFromString: @ "2020-10-31 00:00:00"];

NSDate * date = [NSDate date];

// Calculation interval (in seconds)

NSTimeInterval time = [date1 timeIntervalSinceDate: date];

// Calculates the number of days, hours, minutes, and seconds.

Int days = (int) time)/(3600*24 );

Int hours = (int) time) % (3600*24)/3600;

Int minutes = (int) time) % (3600*24) % 3600/60;

Int seconds = (int) time) % (3600*24) % 3600% 60;

NSString * dateContent = [[NSString alloc] initWithFormat: @ "Only % I days % I hours % I minutes % I seconds", days, hours, minutes, seconds];

(% I can automatically convert the input to decimal, while % d won't convert)

// Value assignment display

UILabel * timeLab = (UILabel *) [self. view viewWithTag: 666666];

TimeLab. text = dateContent;

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.