2011-12-05 18 views
5

Lo que sigue debe dame un objeto NSDate de 9:30:obtiene la hora actual en el iPhone en un formato elegido

NSString *dateString = @"09-30"; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"hh-mm"]; 
NSDate *dateFromString = [[NSDate alloc] init]; 
dateFromString = [dateFormatter dateFromString:dateString]; 

¿Cómo llego la hora actual en el mismo formato?

Respuesta

15
NSDate *currentTime = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"hh-mm"]; 
NSString *resultString = [dateFormatter stringFromDate: currentTime]; 
+1

NSString * CadenaResultado = [dateFromatter stringFromDate: horaActual]; cambiar fechaFromatter to dateFormatter –

+0

thx para detectar errores tipográficos – Denis

2
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"hh-mm"]; 
NSDate *currentDate = [NSDate date]; 
NSString *currentDateString = [dateFormatter stringFromDate:currentDate]; 
0
NSDate *currentDate =[NSDate date]; 
//Your formatter goes here. 
NSString *currentTime = [dateFormatter stringFromDate:currentDate]; 
1

NSDate alloc init creará una fecha con la hora actual. Luego, puede usar NSDateFormatter stringFromDate para formatear y presentar esa fecha.

Este:

NSDate *date = [[NSDate alloc] init]; 
NSLog(@"%@", date); 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"hh-mm"]; 
NSString *dateString = [dateFormatter stringFromDate:date]; 

NSLog(@"%@", dateString); 

Salidas:

2011-12-05 07:20:02.994 Craplet[1706:707] 2011-12-05 12:20:02 +0000 
2011-12-05 07:20:02.995 Craplet[1706:707] 07-20 
1
NSDate *today = [[NSDate alloc] init]; 
NSLog(@"today is :%@", date); 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"hh-mm:ss"]; 
NSString *timeString = [dateFormatter stringFromDate:today]; 

NSLog(@"%@", timeString); 
Cuestiones relacionadas