如何在目标c中找到HH:MM:SS格式的两个日期之间的差异? [英] how to find the difference between two dates in HH:MM:SS format in objective c?

查看:100
本文介绍了如何在目标c中找到HH:MM:SS格式的两个日期之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为标题本身说明了我想做什么?

I think the heading itself says what i want to do?

Stil,请让我澄清一下.我现在正在处理一个应用程序,其中有两个日期格式如下:

Stil,let me clarify. I am right now working on an application where i have two dated in a format like :

"YYYY:MM:DD HH:MM:SS"

"YYYY:MM:DD HH:MM:SS"

我需要使用哪种格式来计算两个日期之间的差异:

Using which i need to calculate difference between the both dates in the format :

HH:MM:SS

HH:MM:SS

我在Wiki上进行了搜索,也尝试了自己的运气,但徒劳无功.有人可以帮助我吗?

I searched in the wiki and also tried my luck but in vain.Can anybody help me out?

谢谢.

推荐答案

以下是我认为您需要执行的步骤:

Here's the steps that I think you'll need to perform:

  1. 使用NSDate对象/Reference.html"rel =" noreferrer> NSDateFormatter .

  1. Parse both the dates into NSDate objects using an NSDateFormatter.

使用

NSTimeInterval ti = [laterDate timeIntervalSinceDate:earlierDate];

  • 上面的ti变量现在包含两个日期之间的秒数.您可以使用以下方法将其重新格式化为小时,秒和分钟:

  • The ti variable above now contains the number of seconds between the two dates. You can reformat that into hours, seconds and minutes with:

    NSUInteger h, m, s;
    h = (ti / 3600);
    m = ((NSUInteger)(ti / 60)) % 60;
    s = ((NSUInteger) ti) % 60;
    NSString *str = [NSString stringWithFormat:@"%lu:%02lu:%02lu", h, m, s];
    

  • 免责声明

    对我来说很晚,所以上述关于小时,分钟和秒的计算可能不正确.

    Disclaimer

    It is late at night for me so the above calculations for hours, minutes and seconds may not be correct.

    这篇关于如何在目标c中找到HH:MM:SS格式的两个日期之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆