开发者

Long to nsdate in Objective C

I have an char arr开发者_Python百科ay of 8 bytes which contains current date in long int. How can I convert this into NSDate.


[NSDate date]

will return the current date.

alternatively, see NSDateFormatter to convert it to a string using format specification and back. beyond that... we'll need to know the internal representation of your char array.


NSDate offers initWithTimeIntervalSinceReferenceDate. The trick is to get the 'reference date' correct. By default, Apple uses 1 January 2001.

Jeff


If you have something like this=> 20100914

char arr[] = {'2','0','1','0','0','9','1','4','\0'};

NSString *dateString = [NSString stringWithCString:arr encoding:NSASCIIStringEncoding];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"yyyyMMdd"];
NSDate *date = [df dateFromString:dateString];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜