[英]How can I convert system millisecond to the Date in objective-c
我从Web服务获取日期,如/ Date(1326067200000)/,如何将其转换为DD.MM.YYYY之类的日期?
You can use 您可以使用
NSString *actDate = @"/Date(1326067200000)/";
NSString *nDate = [[[[actDate componentsSeparatedByString:@"("] objectAtIndex:1] componentsSeparatedByString:@")"] objectAtIndex:0];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:([nDate doubleValue] / 1000)];
In date
you will get the actual date. date
您将获得实际日期。 Further you can format it in "MM/dd/yyyy" format by using 您还可以使用“MM / dd / yyyy”格式对其进行格式化
NSDateFormatter *dtfrm = [[NSDateFormatter alloc] init];
[dtfrm setDateFormat:@"MM/dd/yyyy"];
nDate = [dtfrm stringFromDate:date];
In nDate
you will get your desired date in a formatted way. 在
nDate
您将以格式化的方式获得所需的日期。
//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;
}
Use NSDate's initWithTimeIntervalSinceReferenceDate: (assuming your value uses the first instant of 1 January 2001, GMT as reference date) to get a NSDate instance representing your timestamp. 使用NSDate的initWithTimeIntervalSinceReferenceDate :(假设您的值使用2001年1月1日的第一个时刻,GMT作为参考日期)来获取表示您的时间戳的NSDate实例。
To get a formatted string representation, you can use the class NSDateFormatter . 要获取格式化的字符串表示形式,可以使用NSDateFormatter类。
Try this... 尝试这个...
NSDate *date=[[NSDate alloc] initWithTimeIntervalSinceReferenceDate:gettingdate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSString *newdate = [dateFormat stringFromDate:date];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.