简体   繁体   中英

DateTime Objects to NSDate/NSString in iOS

I get date in this format from web service ( oData Web Service)

/Date(847065600000)

According to this ques. How to handle json DateTime returned from WCF Data Services (OData)

This is a DateTime Object. How can I convert it to a valid iOS date format?

That number is a unix timestamp ie the seconds passed since 1970. You can convert it to NSDate with its initializer:

  NSTimeInterval timeInterval = 847065600000;
  NSDate* date = [NSDate dateWithTimeIntervalSince1970:timeInterval];

UPDATE:

I checked your number and it seems that it is actually in milliseconds. So you should divide it by 1000 before passing to dateWithTimeIntervalSince1970: to get the correct date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM