Objective-c中相当于java时间戳的是什么?

发布于 2024-09-09 22:31:47 字数 203 浏览 2 评论 0原文

我在任何地方都没有找到这个问题的答案,但这似乎是一个典型的问题:我在 Objective-C 中有一个“NSDate 时间戳”,看起来像“2010-07-14 16:30:41 +0200”。 java时间戳只是一个长整数(例如:“976712400000”)。

所以,我的问题是:什么是 Objective-c 相当于 java 时间戳?

预先感谢您的帮助。

I've not found a answer to this question anywhere, but this seems like a typical problem: I have in Objective-C a "NSDate timestamp" that looks like "2010-07-14 16:30:41 +0200". The java timestamp is just a long integer (for example:"976712400000").

So, my question is: What is a Objective-c equivalent to java timestamp?

Thanks in advance for helping.

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

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

发布评论

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

评论(2

最单纯的乌龟 2024-09-16 22:31:47

虽然@lordsandwich的答案是正确的,但您也可以直接使用NSDate timeIntervalSince1970方法,而不是自己“制作”1970年的NSDate

它的工作原理如下:

NSDate *past = [NSDate date];
NSTimeInterval oldTime = [past timeIntervalSince1970];
NSString *unixTime = [[NSString alloc] initWithFormat:@"%0.0f", oldTime];

当您使用此方法时,您不会不必要地向自动释放池添加新对象,因此我认为使用此方法实际上更好。

Although @lordsandwich's answer is correct, you can also directly use the NSDate timeIntervalSince1970 method, instead of 'making' the 1970 NSDate yourself.

That would work like this:

NSDate *past = [NSDate date];
NSTimeInterval oldTime = [past timeIntervalSince1970];
NSString *unixTime = [[NSString alloc] initWithFormat:@"%0.0f", oldTime];

As when you use this you don't unnecessarily add a new object to the autorelease pool, I think it's actually better to use this method.

み格子的夏天 2024-09-16 22:31:47

您可以通过减去起始时间来将 NSDAte 为您提供的格式转换为 unix 时间
unix 时间的日期,即 1970 年 1 月 1 日。 NSTimeInterval 只是两个日期之间的差异,您可以以秒数为单位获取该值:

NSDate * past = [NSDate date];
NSTimeInterval oldTime = [past timeIntervalSinceDate:[NSDate dateWithNaturalLanguageString:@"01/01/1970"]];
NSString * unixTime = [[NSString alloc] initWithFormat:@"%0.0f", oldTime];

You can convert the format that NSDAte gives you to unix time by substracting the starting
date of unix time which is the 1st of January 1970. NSTimeInterval is simply the difference between two dates and you can get that in number of seconds:

NSDate * past = [NSDate date];
NSTimeInterval oldTime = [past timeIntervalSinceDate:[NSDate dateWithNaturalLanguageString:@"01/01/1970"]];
NSString * unixTime = [[NSString alloc] initWithFormat:@"%0.0f", oldTime];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文