如何在没有日期的 NSDate 中存储时间?

发布于 2024-11-11 12:26:43 字数 1013 浏览 6 评论 0原文

我的应用程序中有一个计时器。当我单击退出按钮时,计时器停止并将值以 01:15:55 的格式存储到字符串中。我有一个数组来存储这个字符串对象。

我想要的是,现在我想通过相互比较来显示这些值。所以我想首先我必须将字符串转换为 NSDate 但我只有时间格式并且不想存储日期。

我怎样才能完成这个任务?有什么建议吗?

编辑:代码

NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:sDate]; // sDate = when app get started 

myAppDelegate.seconds = secondsSinceStart % 60;
myAppDelegate.minutes = (secondsSinceStart / 60) % 60;
myAppDelegate.hours = secondsSinceStart / (60 * 60);
NSString *result = nil;

if (myAppDelegate.hours > 0) 
{
    result = [NSString stringWithFormat:@"%02d:%02d:%02d", myAppDelegate.hours, myAppDelegate.minutes, myAppDelegate.seconds];
}
else 
{
    result = [NSString stringWithFormat:@"%02d:%02d", myAppDelegate.minutes, myAppDelegate.seconds];        
}

NSString *tempDateString = [NSString stringWithFormat:@"%d:%d:%d",[myAppDelegate hours],[myAppDelegate minutes],[mogsApp seconds]];

现在我想将 tempDateString 转换为 NSDate,以便我可以与类似的对象进行比较。是否可以 ?

谢谢...

I have a timer in my app. When I click on exit buton then timer gets stop and stores value into the string in format of 01:15:55 . I have an array to store this string object.

What I want is , now I want to display these values by comparing to each other. So I think first I have to convert the string into the NSDate but I am having only time format and do not want to store date.

How can I accomplish this task ? any suggestion ?

EDITED : code

NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:sDate]; // sDate = when app get started 

myAppDelegate.seconds = secondsSinceStart % 60;
myAppDelegate.minutes = (secondsSinceStart / 60) % 60;
myAppDelegate.hours = secondsSinceStart / (60 * 60);
NSString *result = nil;

if (myAppDelegate.hours > 0) 
{
    result = [NSString stringWithFormat:@"%02d:%02d:%02d", myAppDelegate.hours, myAppDelegate.minutes, myAppDelegate.seconds];
}
else 
{
    result = [NSString stringWithFormat:@"%02d:%02d", myAppDelegate.minutes, myAppDelegate.seconds];        
}

NSString *tempDateString = [NSString stringWithFormat:@"%d:%d:%d",[myAppDelegate hours],[myAppDelegate minutes],[mogsApp seconds]];

Now I want to convert tempDateString into the NSDate so I can compare with similar objects. Is it possible ?

Thanks...

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

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

发布评论

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

评论(2

被你宠の有点坏 2024-11-18 12:26:43

听起来 NSTimeInterval 可能更合适。这只是一个浮点值,表示秒数(包括秒的小数部分)。您可以通过一些简单的除法和余数数学将这样的值手动格式化为您想要的任何字符串格式。 (如果您想使用参考日期或其他日期来获取值,NSDate 将为您提供自参考日期或其他日期以来的时间间隔。)如果需要,您可以将 NSTimeIntervals 存储为字符串。

Sounds like an NSTimeInterval might be more appropriate. This is just a floating-point value indicating a number of seconds (including fractional seconds). You can manually format a value like this into whatever string format you want with some simple division and remainder math. (NSDate will give you time intervals since a reference date or other dates if you want to use those to get the values.) You can store NSTimeIntervals as strings if necessary.

轻拂→两袖风尘 2024-11-18 12:26:43

NSDateComponents始终是一个不错的选择。

它还可以让您通过 NSCalendar。然后(与使用 NSTimeInterval 不同),您不必自己设置任何数学,它都会自动本地化。

NSDateComponents is always a good choice when storing only parts of a date/time (or a timespan).

It also gives you easy access to time management methods via NSCalendar. Then (unlike using NSTimeInterval), you don't have to set up any of the math yourself, and it will all automagically localize.

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