本文介紹了將 .NET 中的 DateTime 對象格式化為 Objective-c 的 NSDate的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用一個將 .NET DateTime 對象返回到我的 iOS 應用程序的 API.我對發生的事情有點困惑,DateTime 在離開 API 時看起來很好,但是當它進入時,它會通過 JSON 并以如下所示的字符串形式進入:
I am working with an API that returns a .NET DateTime object into my iOS application. I'm a little confused at what's going on, the DateTime looks fine when it leaves the API, but when it comes in it goes through JSON and comes in as a string that looks like this:
/Date(1303884000000-0600)/
WTF 是這樣的,我怎樣才能把它變成一個 NSDate 對象??
WTF is that and how can I turn it into a NSDate object??
謝謝!
推薦答案
來自 解析 JSON 日期iPhone 我覺得下面這個功能很完美:
From Parsing JSON dates on IPhone I found the following function to be perfect:
- (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];
}
這篇關于將 .NET 中的 DateTime 對象格式化為 Objective-c 的 NSDate的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!