简体   繁体   English

iOS中的NSDate / NSString的DateTime对象

[英]DateTime Objects to NSDate/NSString in iOS

I get date in this format from web service ( oData Web Service) 我从Web服务(oData Web服务)以这种格式获取日期

/Date(847065600000)

According to this ques. 根据这个问题。 How to handle json DateTime returned from WCF Data Services (OData) 如何处理从WCF数据服务(OData)返回的json DateTime

This is a DateTime Object. 这是一个DateTime对象。 How can I convert it to a valid iOS date format? 如何将其转换为有效的iOS日期格式?

That number is a unix timestamp ie the seconds passed since 1970. You can convert it to NSDate with its initializer: 该数字是unix时间戳,即自1970年以来经过的秒数。您可以使用其初始化程序将其转换为NSDate:

  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. 因此,在传递给dateWithTimeIntervalSince1970:之前,应将其除以1000,以获取正确的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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