2012-01-10 34 views

Trả lời

16

Bạn có thể sử dụng

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

Trong date bạn sẽ có được những ngày thực tế. Ngoài ra bạn có thể định dạng nó ở định dạng "MM/dd/yyyy" bằng cách sử dụng

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

Trong nDate bạn sẽ nhận được ngày mong muốn của bạn theo một cách định dạng.

+0

Cảm ơn bạn rất nhiều. Nó hoạt động tuyệt vời! – OzBoz

0

Sử dụng NSDate's initWithTimeIntervalSinceReferenceDate: (giả sử giá trị của bạn sử dụng ngày đầu tiên của ngày 1 tháng 1 năm 2001, GMT làm ngày tham chiếu) để có được một thể hiện NSDate đại diện cho dấu thời gian của bạn.

Để có được biểu diễn chuỗi được định dạng, bạn có thể sử dụng lớp NSDateFormatter.

0

Hãy thử này ...

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' đang mong đợi một NSString, nhưng bạn đang gán một đối tượng NSDate. 'AllKeys' là gì và nó phải làm gì với'/Date (1326067200000)/' – vikingosegundo

Các vấn đề liên quan