2010-02-22 17 views

Antwort

184
NSString *dateStr = @"20100223"; 

// Convert string to date object 
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"yyyyMMdd"]; 
NSDate *date = [dateFormat dateFromString:dateStr]; 

// Convert date object to desired output format 
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"]; 
dateStr = [dateFormat stringFromDate:date]; 
[dateFormat release]; 

Hoffe, das wird Ihnen helfen.

+14

einen Artikel gefunden, die hier alle Formatwerte erklärt: http://www.alexcurylo.com/blog/2009/01/29/nsdateformatter-formatting/ – mtmurdock

+0

Auch unicode.org Eintrag beachten Sie: http: // www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns –

+0

Wissen Sie, warum kleiner Buchstabe yyyy und dd funktioniert und Großbuchstabe MM funktioniert ...? –

8

Sie sollten sich einen Blick auf NSDateFormatter werfen. Ermitteln Sie das Format der Datumszeichenfolge und verwenden Sie dann dateFromString:, um die Zeichenfolge in ein NSDate-Objekt zu konvertieren.

4
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"dd/MM/YYYY"]; 
NSDate *date = [dateFormat dateFromString:dateStr]; 
list = [self getYear:date]; 


- (NSMutableArray *)getYear:(NSDate*)date 
{ 
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date]; 

    int year = [components year]; 
    int month = [components month]; 
    int day = [components day]; 
    NSLog(@"%d",year); 

    NSMutableDictionary *dateDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d", day], @"day", [NSString stringWithFormat:@"%d", month], @"month", [NSString stringWithFormat:@"%d", year], @"year", nil]; 

    return dateDict; 
} 
+2

'YYYY' hat fast nie Recht. – vikingosegundo

0
+(NSDate*)str2date:(NSString*)dateStr{ 
    if ([dateStr isKindOfClass:[NSDate class]]) { 
     return (NSDate*)dateStr; 
    } 

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"yyyy-MM-dd"]; 
    NSDate *date = [dateFormat dateFromString:dateStr]; 
    return date; 
} 
1

A simple approach auch wenn Sie SQLite DB:

// NSTimeInterval is basically typedef of double so you can receive it as double also. 
NSTimeInterval *current = [[NSDate date] timeIntervalSince1970]; 
[DB addToDB:current]; 

//retrieve the date from DB 
NSDate *retrievedDateItem = [[NSDate alloc] initWithTimeIntervalSince1970:[getDBValueAsDouble]]; //convert it from double (NSTimeInterval) to NSDate 
// Set the style 
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; 
// Converted to string 
NSString *convertedDate = [dateFormatter stringFromDate:retrievedDateItem]; 

Die Ausgabe für dieses Beispiel:

3. August 2015, 12.27.50

0

Meine Arbeitsversion in Swift

func dateFor(timeStamp: String) -> NSDate 
{ 
    let formater = NSDateFormatter() 
    formater.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy" 
    return formater.dateFromString(timeStamp)! 
} 


func timeStampFor(date: NSDate) -> String 
{ 
    let dateFormatter = NSDateFormatter() 
    dateFormatter.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy" 

    return dateFormatter.stringFromDate(date) 
} 
Verwandte Themen