2012-11-22 9 views
16

Chcę wyświetlić datę w formacie "20 września 2012" z ciągu "2012-11-22 10:19:04". Jak mogę to zrobić? Czy jest jakaś wbudowana metoda dla iOS?iOS: format daty - 20 września 2012 r.

+0

tak można ustawić format daty dd-mmm-rrrr –

+0

Jak mogę to zrobić? Czy możesz podać kod? – Dev

Odpowiedz

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]); 

Twój dziennik zostanie wydrukowany w ten sposób. 22-Nov-2012

+0

co jeśli potrzebuję '2012-11-22 at 11: 23'? jak dodać, że 'at' inbetween –

+0

@ EICaptainv2.0 Możesz umieścić' at' wewnątrz formatu. "rrrr-MM-dd" w "GG: mm" Powinno działać. Możesz umieścić tekst w cudzysłowie, aby go odzyskać. –

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

To odpowiada tylko na drugą połowę pytania plus używasz niewłaściwych specyfikatorów formatu. – rmaddy

6

Spróbuj tego,

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 

następnie użyć go do analizowania ciąg decyduje data,

NSDate *date = [dateFormatter dateFromString:originalDateString]; 

Można utworzyć nowy dateFormatter drukować w nowym formacie lub po prostu ponownie wykorzystać istniejący jako,

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

zmiana mate // 20-wrz-2012 na 22-lis-2012 mate .. :) –

+0

@ParasJoshi, Dzięki. Właśnie przytaczałem jego pytanie, tylko dla jego zrozumienia. Po prostu dać mu znać, że jest to równoważny format. – iDev

0

spróbować

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 

zobaczyć mojego bloga z tym wszelkie informacje My Blog Link with this types of method

0

Można to osiągnąć w następujący sposób

 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 

Ciesz Programowanie

2

Akceptowane odpowiedź od (@Dinesh Raja) w 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) 
    }