iphone sdk更新时间
-
21-08-2019 - |
题
我正在使用下面提供的代码来显示时间和日期。谁能帮我自动更改时间(按秒)和日期(按天)?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(@"\n"
"theDate: |%@| \n"
"theTime: |%@| \n"
, theDate, theTime);
[dateFormat release];
[timeFormat release];
[now release];
解决方案
具体来说,您可以使用 NSTimer ScheduledTimerWithTimeInterval:目标:选择器:用户信息:重复:. 。您可以使用 1 作为时间间隔。
其他提示
使用NSDateComponents
类。例如,为一天和至少一个第二添加到日期:
NSDate *startDate = [NSDate date];
unsigned unitFlags = NSDayCalendarUnit | NSSecondCalendarUnit;
NSCalendar *curr = [NSCalendar currentCalendar];
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
oneDay.day = 1;
oneDay.second = 1;
NSDate* adjustedDate = [curr dateByAddingComponents:oneDay
toDate:startDate
options:0];
不隶属于 StackOverflow