Pergunta

Eu estou usando o código fornecido abaixo para tempo de exibição e data. Alguém pode me ajudar com atuomatically alterar o tempo de segundos e a data a cada dia?

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];
Foi útil?

Solução

Você pode usar NSTimer, especificamente scheduledTimerWithTimeInterval: alvo: seletor: userInfo: repete: . Você pode usar 1 como um intervalo de tempo.

Outras dicas

Use a classe NSDateComponents. Por exemplo, para adicionar um dia e um segundo para uma data:

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]; 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top