简体   繁体   English

减少Objective-C中的时间

[英]Decrease time in objective-c

I have a number of hours and a number of minutes, and I want a reduce the time of 1 second. 我有几个小时和几分钟,并且我希望减少1秒的时间。 So, I think I must create a nsdate object with my time, and after find the method to decrease the time. 因此,我认为我必须用自己的时间创建一个nsdate对象,然后找到减少时间的方法。 I looked in the apple documentation but I don't find how to do this. 我查看了Apple文档,但找不到如何执行此操作。

You can use this 你可以用这个

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
NewDateString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:-1]

You want NSDateComponents . 您需要NSDateComponents Note, you need to use it with a specific calendar. 请注意,您需要将其用于特定的日历。 A code example is listed on the page to which I linked. 我链接到的页面上列出了一个代码示例。

Don't reinvent the wheel. 不要重新发明轮子。 There are many open source NSDate additions that you can use here. 您可以在此处使用许多开源的NSDate附加功能。 See here 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM