gpt4 book ai didi

iphone - 关于 JSON 日期转换错误

转载 作者:行者123 更新时间:2023-11-28 22:36:52 27 4
gpt4 key购买 nike

我得到 JSON 解析日期,我想使用 NSDateFormatter,但没有成功。

1.JSON获取控制台日志

IncidentReceiveTime = "/Date(1353914100000+0800)/";

2.我的代码

NSString *dateStr =[NSString stringWithFormat:@"%@",[[[DateSortArry objectAtIndex:0] objectForKey:@"IncidentReceiveTime"]stringByReplacingOccurrencesOfString:@"/" withString:@""]];
NSLog(@"dateStr:%@",dateStr);

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"yyyy-MM-ddHH"];
NSDate *date = [dateFormatter dateFromString:dateStr];
NSLog(@"date:%@",date);

3.NSDateFormatter控制台日志

dateStr:Date(1361943694000+0800)
date:(null)

最佳答案

引用我的回答:

iPhone:How to convert Regular Expression into Date Format?

在这里,我在上面的链接中发布我的答案中的这段代码,好像万一将来上面的链接失效,那么这个答案就没有用了:

- (NSDate*) getDateFromJSON:(NSString *)dateString
{
// Expect date in this format "/Date(1268123281843)/"
int startPos = [dateString rangeOfString:@"("].location+1;
int endPos = [dateString rangeOfString:@")"].location;
NSRange range = NSMakeRange(startPos,endPos-startPos);
unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];
NSLog(@"%llu",milliseconds);
NSTimeInterval interval = milliseconds/1000;
return [NSDate dateWithTimeIntervalSince1970:interval];
}

一旦你从上面的方法中得到了 NSDate 对象:

NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *dateStr = [dateForm stringFromDate:<YourNSDateObject>];
[dateForm setDateFormat:@"<Your Desired Date Format>"];
NSDate *desireddate = [dateForm dateFromString:dateStr];

希望这对您有所帮助。

关于iphone - 关于 JSON 日期转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com