开发者

Long to nsdate in Objective C

开发者 https://www.devze.com 2023-01-16 05:53 出处:网络
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]

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

精彩评论

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