iOS Development calculates a time interval of two dates

Source: Internet
Author: User
Tags date1

First create a formatted object

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

[Dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];

Then create a Date object

NSDate *date1 = [dateformatter datefromstring:@ "2020-10-31 00:00:00"];

NSDate *date = [NSDate Date];

Calculate time interval (units in seconds)

Nstimeinterval time = [Date1 timeintervalsincedate:date];

Calculate days, hours, minutes, 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];

(The%i can automatically convert the input to decimal, and%d does not convert)

Assign value Display

UILabel *timelab = (UILabel *) [Self.view viewwithtag:666666];

Timelab.text = datecontent;

iOS Development calculates a time interval of two dates

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.