views:

1198

answers:

2

Does anyone know how to convert a JSON date(ticks) to an NSDate in Objective-C? Can someone post some code?

A: 

I'm guessing here but your JSON value is the number of milliseconds since 1970, right? You can use NSDate's dateWithTimeIntervalSince1970: method to return an NSDate object with the correct time. Just make sure to convert the JSON milliseconds number to seconds before passing it to NSDate-- Cocoa uses NSTimeInterval in most places, which represents an interval in seconds.

Marc Charbonneau
Thanks Marc. That worked for me. Do you know how to display the NSDate to the local time zone of the client?
Also, do you know how go from NSDate back Date(ticks) to prepare the JSON object to be sent to a .net webservice?
A: 

You'd have to detect the client's locale in order to be able to do that, and unless your client knows how to do that, there's probably not much point.

NSDate's descriptionWithLocale: would be the way you format it for another locale. And timeIntervalSince1970 will go back to the (seconds) since 1970, which you could multiply by 1000 to get ms to return to the client. It's all in the NSDate documentation.

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

AlBlue