2012-01-10 8 views

risposta

16

È possibile utilizzare

NSString *actDate = @"/Date(1326067200000)/"; 
    NSString *nDate = [[[[actDate componentsSeparatedByString:@"("] objectAtIndex:1] componentsSeparatedByString:@")"] objectAtIndex:0]; 
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:([nDate doubleValue]/1000)]; 

In date si otterrà la data effettiva. Inoltre è possibile formattare in formato "MM/gg/aaaa" utilizzando

NSDateFormatter *dtfrm = [[NSDateFormatter alloc] init]; 
    [dtfrm setDateFormat:@"MM/dd/yyyy"]; 
    nDate = [dtfrm stringFromDate:date]; 

In nDate avrete la vostra data desiderata in modo formattato.

+0

Grazie mille. Funziona alla grande! – OzBoz

0

Utilizzare NSDate's initWithTimeIntervalSinceReferenceDate: (supponendo che il valore utilizzi il primo istante del 1 ° gennaio 2001, GMT come data di riferimento) per ottenere un'istanza NSDate che rappresenta il timestamp.

Per ottenere una rappresentazione di stringa formattata, è possibile utilizzare la classe NSDateFormatter.

0

Prova questo ...

NSDate *date=[[NSDate alloc] initWithTimeIntervalSinceReferenceDate:gettingdate]; 
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; 
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"]; 
NSString *newdate = [dateFormat stringFromDate:date]; 
0
//Use of Function 
textFiled.text = [self displayDate:[[NSString stringWithFormat:@"%@",[[[AllKeys valueForKey:@"Date"] componentsSeparatedByString:@"\"]]]] 


// Millisecond to Date conversion 
- (NSDate *)displayDate:(double)unixMilliseconds { 

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:unixMilliseconds/1000.]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setLocale:[NSLocale currentLocale]]; 

    NSLog(@"The date is %@", [dateFormatter stringFromDate:date]); 
    // NSDate *returnValue = [dateFormatter stringFromDate:date]; 
    return date; 
} 
+0

'textField.text' sta aspettando una NSString, ma si assegna un oggetto NSDate. Che cosa è 'AllKeys' e che cosa ha a che fare con'/Date (1326067200000)/' – vikingosegundo

Problemi correlati