我从网络服务获取日期,格式为/Date(1326067200000)/,如何将其转换为DD.MM.YYYY格式的日期?
我从网络服务获取日期,格式为/Date(1326067200000)/,如何将其转换为DD.MM.YYYY格式的日期?
NSString *actDate = @"/Date(1326067200000)/";
NSString *nDate = [[[[actDate componentsSeparatedByString:@"("] objectAtIndex:1] componentsSeparatedByString:@")"] objectAtIndex:0];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:([nDate doubleValue] / 1000)];
在date
中,你将获得实际日期。你可以使用以下方式将其格式化为“MM/dd/yyyy”格式
NSDateFormatter *dtfrm = [[NSDateFormatter alloc] init];
[dtfrm setDateFormat:@"MM/dd/yyyy"];
nDate = [dtfrm stringFromDate:date];
在nDate
中,您将以格式化的方式获取所需的日期。
试试这个...
NSDate *date=[[NSDate alloc] initWithTimeIntervalSinceReferenceDate:gettingdate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSString *newdate = [dateFormat stringFromDate:date];
//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;
}
textField.text
期望一个 NSString 类型,但你正在赋值一个 NSDate 对象。什么是 AllKeys
,它与 /Date(1326067200000)/
有什么关系? - vikingosegundo