2012-11-22 9 views
16

Desidero visualizzare la data nel formato "20-Sep-2012" dalla stringa "2012-11-22 10:19:04". Come posso fare questo? Esiste un metodo integrato per iOS?iOS: Formato data - 20-set-2012

+0

sì, si può impostare il formato della data a dd-mmm-aaaa –

+0

Come posso fare? Puoi fornire il codice? – Dev

risposta

55
NSString *myString = @"2012-11-22 10:19:04"; 
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";  
NSDate *yourDate = [dateFormatter dateFromString:myString]; 
dateFormatter.dateFormat = @"dd-MMM-yyyy"; 
NSLog(@"%@",[dateFormatter stringFromDate:yourDate]); 

il tuo registro verrà stampato in questo modo. 22-Nov-2012

+0

cosa succede se ho bisogno di '2012-11-22 alle 11: 23'? come aggiungo che 'at' in between –

+0

@ EICaptainv2.0 Puoi mettere' at' all'interno del formato. 'aaaa-MM-gg 'a' HH: mm' Questo dovrebbe funzionare. Puoi inserire del testo tra virgolette singole per riaverlo. –

-3
NSDate *today = [NSDate date]; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd-mmm-yyyy"]; 
    NSString *dateString = [dateFormat stringFromDate:today]; 
    [dateFormat release]; 
+1

Questo risponde solo alla seconda metà della domanda, più si stanno utilizzando gli specificatori di formato errati. – rmaddy

6

Prova questo,

NSString *originalDateString = @"2012-11-22 10:19:04"; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//2012-11-22 10:19:04 

quindi utilizzarlo per analizzare la stringa data,

NSDate *date = [dateFormatter dateFromString:originalDateString]; 

È possibile creare un nuovo dateformatter per stampare nel nuovo formato o semplicemente riutilizzare il esistente come,

[dateFormatter setDateFormat:@"dd-MMM-yyyy"];//22-Nov-2012 
NSString *formattedDateString = [dateFormatter stringFromDate:date]; 
NSLog(@"Date in new format is %@", formattedDateString); 
+0

mate change // 20-set-2012 to 22-nov-2012 mate .. :) –

+0

@ParasJoshi, grazie. Stavo solo citando la sua domanda lì, solo per la sua comprensione. Solo per fargli sapere che il formato equivalente è quello. – iDev

0

provare questo

NSString *str = @"2012-11-22 10:19:04"; 

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

    NSDate *date = [dateFormatter dateFromString: str]; 

    dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"dd-MMM-yyyy"]; 

    NSString *convertedString = [dateFormatter stringFromDate:date]; 
    NSLog(@"Converted String : %@",convertedString);// Print Log (Output) is 22-Nov-2012 

vedere il mio blog con questo tutte le informazioni My Blog Link with this types of method

0

È possibile raggiungere questo obiettivo con modo seguente

 NSDateFormatter *dfTime = [[NSDateFormatter alloc] init]; 
     [dfTime setDateFormat:@"yyyy--MM-dd HH:mm:ss"]; 
     NSDate *dtTime = [dfTime dateFromString:<Your string>]; 
     NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
     [df setDateFormat:@"dd-MMM-yyyy"]; 
     <Your result string> = [df stringFromDate:dtTime]; 

Where <Your string> = @"2012-11-22 10:19:04" & 
<Your result string> will be your new string 

una programmazione

2

risposta accettata da (@Dinesh Raja) in Swift 3:

override func viewDidLoad() { 
super.viewDidLoad() 
    print(self.getFormattedDate(myString: "2012-11-22 10:19:04")) 
} 

func getFormattedDate(myString: String) -> String{ 
     let dateFormatter = DateFormatter() 
     dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" 
     let yourDate = dateFormatter.date(from: myString)! 
     dateFormatter.dateFormat = "dd-MMM-yyyy" 
     return dateFormatter.string(from: yourDate) 
    }