문제

JSON 날짜 (진드기)를 Objective-C의 NSDate로 변환하는 방법을 아는 사람이 있습니까? 누군가 코드를 게시 할 수 있습니까?

도움이 되었습니까?

해결책

나는 여기서 추측하고 있지만 당신의 JSON 가치는 1970 년 이후 밀리 초의 수입니다. NSDATE를 사용할 수 있습니다 dateWithTimeIntervalSince1970: 방법 올바른 시간으로 nsdate 객체를 반환하는 방법. JSON 밀리 초 숫자를 NSDATE로 전달하기 전에 몇 초로 변환하십시오. Cocoa는 대부분의 장소에서 nstimeinterval을 사용합니다. 이는 몇 초 만에 간격을 나타냅니다.

다른 팁

대략 이것은 다음과 같습니다.

// Input string is something like: "/Date(1292851800000+0100)/" where
// 1292851800000 is milliseconds since 1970 and +0100 is the timezone
NSString *inputString = [item objectForKey:@"DateTimeSession"];

// This will tell number of seconds to add according to your default timezone
// Note: if you don't care about timezone changes, just delete/comment it out
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];

// A range of NSMakeRange(6, 10) will generate "1292851800" from "/Date(1292851800000+0100)/"
// as in example above. We crop additional three zeros, because "dateWithTimeIntervalSince1970:"
// wants seconds, not milliseconds; since 1 second is equal to 1000 milliseconds, this will work.
// Note: if you don't care about timezone changes, just chop out "dateByAddingTimeInterval:offset" part
NSDate *date = [[NSDate dateWithTimeIntervalSince1970:
                 [[inputString substringWithRange:NSMakeRange(6, 10)] intValue]]
                dateByAddingTimeInterval:offset];

(에서 https://gist.github.com/726910)

그렇게하려면 고객의 로케일을 감지해야하며, 고객이 그렇게하는 방법을 알지 못하면 아마도 많은 점이 없을 것입니다.

NSDATE의 DescriptionWithLocale : 다른 로케일에 대한 포맷 방법이 될 것입니다. 그리고 TimeIntervalsince1970은 1970 년부터 (초)로 돌아가서 MS가 클라이언트로 돌아 오도록 1000을 곱할 수 있습니다. NSDate 문서에 있습니다.

http://developer.apple.com/documentation/cocoa/reference/foundation/classes/nsdate_class/reference/reference.html

이 페이지에 따르면 : http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx 진드기는 1 월 1 일에 시작됩니다 dateWithTimeIntervalSince1970: 진드기로 작동하도록 자동으로 설정되지 않습니다. 여전히이 방법을 사용할 수 있지만 차이를 조정해야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top