Pregunta

Estoy utilizando el código proporcionado a continuación para mostrar la hora y la fecha. alguien me puede ayudar con el cambio de atuomatically el tiempo por segundo y la fecha en el día?

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

Solución

Puede utilizar NSTimer, específicamente scheduledTimerWithTimeInterval: objetivo: selector: userInfo: repite: . Se puede utilizar al menos 1 intervalo de tiempo.

Otros consejos

Utilice la clase NSDateComponents. Por ejemplo, para agregar un día y un segundo para una fecha:

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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top