2011-09-08 17 views

risposta

16
NSDate *date = [NSDate date]; //I'm using this just to show the this is how you convert a date 

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateStyle:NSDateFormatterLongStyle]; // day, Full month and year 
[df setTimeStyle:NSDateFormatterNoStyle]; // nothing 

NSString *dateString = [df stringFromDate:date]; [df release]; 
+0

questo metodo does't lavoro .. '[df setDateFormat: NSDateFormatterLongStyle];!' - di errore e qui - '[setTimeFormate df: NSDateFormatterNoStyle]; ' – LightNight

+0

@mazder - scuse - Ho modificato questa volta. Provaci ancora. – Abizern

+0

Grazie, ora è ok .. Ma quando cerco di ottenere la data da una stringa, viene visualizzato 'January 26, 2011 'perché? – LightNight

6
NSString *dateStr = @"2011-08-26 14:14:51"; 

//if you have date then, 
NSString *dateStr = [NSString stringWithFormat:@"%@",yourDate]; 



NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; 
NSDate *date = [dateFormat dateFromString:dateStr]; 
[dateFormat setDateFormat:@"MMMM dd, YYYY"]; 
NSString* temp = [dateFormat stringFromDate:date]; 

[dateFormat release]; 



NSLog(@"%@",temp); 
+0

La stringa formato della data è sbagliata. dovrebbe essere @ "AAAA-MM-gg HH: mm: ss". D si riferisce a giorni in un anno d fa riferimento al giorni del mese – Abizern

+0

thanx va bene per questo .. – mayuur

12
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; 
[df setDateFormat:@"dd LLLL yyyy"]; 
NSString *formattedDate = [df stringFromDate:[NSDate date]]; 
Problemi correlati