我有一个NSString,其格式为@“2013-01-09 06:10:10 +0000”(我从服务器获取它,它不是当前时间)。我想连续增加一秒钟。我可以用一个计时器来做,但如何将时间增加一秒?
答案 0 :(得分:33)
试试这个,
NSDate *correctDate = [NSDate dateWithTimeInterval:1.0 sinceDate:yourDate];
您可以使用NSDateFormatter
从字符串中获取日期。
答案 1 :(得分:2)
在您的约会中添加1秒钟,如下所示..
NSDate *mydate = [NSDate date];
NSTimeInterval secondsInEightHours = 60; // you can add hours and minuts with multiply the numbers with this second..
NSDate *dateEightHoursAhead = [mydate dateByAddingTimeInterval:secondsInEightHours];
答案 2 :(得分:2)
如今(2017)Apple建议将(NS)Calendar
用于各种日期数学
<强>目标C 强>
NSDate *now = [NSDate date];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *nowPlusOneSecond = [currentCalendar dateByAddingUnit:NSCalendarUnitSecond
value:1
toDate:now
options:NSCalendarMatchNextTime];
Swift 3
let now = Date()
let currentCalendar = Calendar.current
let nowPlusOneSecond = currentCalendar.date(byAdding: .second, value: 1, to: now)!
答案 3 :(得分:1)
假设你的日期字符串在var中,称为serverString。你可以用这种方式约会......
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss z"];
NSDate *date = [df dateFromString:serverString];
以这种方式递增:
date = [date dateByAddingTimeInterval:1.0];
答案 4 :(得分:0)
SWIFT 3.x解决方案
# table1
ID|URLS | NAME | ADDRESS |
0 |foo.com | foo | foo 1 |
1 |bar.com | bar | bar 1 |