0

以下のコードを使用して、時刻と日付を表示しています。秒単位の時間と日単位の日付を自動的に変更するのを手伝ってくれる人はいますか?

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];
4

3 に答える 3

3

NSTimer、具体的にはscheduledTimerWithTimeInterval:target:selector:userInfo:repeats:を使用できます。時間間隔として 1 を使用できます。

于 2009-06-29T16:57:58.427 に答える
1

NSDateComponentsクラスを使用します。たとえば、日付に 1 日 1 秒を追加するには、次のようにします。

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]; 
于 2009-06-29T17:49:49.237 に答える
0

日付と時刻のプログラミング ガイド

于 2009-06-29T16:57:40.237 に答える