Objective C 中的 nsdate 还很长

发布于 2024-09-19 19:26:51 字数 66 浏览 1 评论 0原文

我有一个 8 字节的 char 数组,其中包含 long int 形式的当前日期。我怎样才能将其转换为 NSDate.

I have an char array of 8 bytes which contains current date in long int. How can I convert this into NSDate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终陌 2024-09-26 19:26:53

如果你有这样的事情=> 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];

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];
南风起 2024-09-26 19:26:52
[NSDate date]

将返回当前日期。

或者,请参阅 NSDateFormatter 使用格式规范将其转换为字符串并返回。除此之外...我们需要知道 char 数组的内部表示。

[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.

︶葆Ⅱㄣ 2024-09-26 19:26:52

NSDate提供 initWithTimeIntervalSinceReferenceDate。诀窍是确保“参考日期”正确。默认情况下,Apple 使用 2001 年 1 月 1 日。

Jeff

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

Jeff

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文