有谁知道如何将一个JSON日期(蜱)转换为一个NSDate在Objective-C?有人可以张贴一些代码?

有帮助吗?

解决方案

我猜在这里,但你的JSON值是自1970年以来的毫秒数,对不对?您可以使用的NSDate的dateWithTimeIntervalSince1970:方法与正确的时间返回一个NSDate对象。它传递给NSDate--可可使用NSTimeInterval在大多数地方,它代表秒的间歇前请务必以JSON的毫秒数转换成秒。

其他提示

它去大致是这样的:

// 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年以来,这你可以乘以1000得到毫秒返回给客户端。它的所有的NSDate文档。

http://developer.apple的.com /文档/可可/参考/粉底/类别/ NSDate_Class /参考/的reference.html

根据该页面: http://msdn.microsoft .COM / EN-US /库/ system.datetime.ticks.aspx 蜱年1月1,0001开始这样dateWithTimeIntervalSince1970:是不会自动安装与蜱工作。你仍然可以使用这种方法,但应调整的差异。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top