如何在没有日期的 NSDate 中存储时间?
我的应用程序中有一个计时器。当我单击退出按钮时,计时器停止并将值以 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来 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.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 usingNSTimeInterval
), you don't have to set up any of the math yourself, and it will all automagically localize.