简体   繁体   English

NSDate但没有NSTime,如何转换字符串表示的时间(没有日期)

[英]NSDate but no NSTime , how to convert a string representation of time (without date)

My problem is I want to have some way to represent time (without date), like time of day in my iOS app. 我的问题是我希望有一些方法来表示时间(没有日期),比如我的iOS应用程序中的时间。 From a REST api I get strings like "13:12:11" which shows the time something happens, I have used NSDateFormatter to convert NSStrings to NSDates but as far as I can tell it does not accept date formats with just time components like HH:mm:ss [EDIT: you can, see below] 从REST api我得到像“13:12:11”这样的字符串,它显示了发生的事情,我使用NSDateFormatter将NSStrings转换为NSDates但据我所知它不接受日期格式只有像HH:mm:ss这样的时间组件HH:mm:ss [编辑:你可以,见下文]

So my questions are 1- Is NSTimeInterval (instead of NSDate) what I should be using to store time of day? 所以我的问题是1-是NSTimeInterval(而不是NSDate)我应该用什么来存储时间?
2- How can I convert "03:04:05" to and objective-c object from one of the built in frameworks. 2-如何将“03:04:05”转换为来自其中一个内置框架的objective-c对象。

EDIT: You CAN use formats like "HH:mm:ss" it just replaces the date part with 2000-01-01 Still it would be very nice to have a date independent time of day representation. 编辑:您可以使用像“HH:mm:ss”这样的格式,它只是用2000-01-01替换日期部分仍然很高兴有一个独立于日期的时间表示。

OK, thanks everybody, this is what I ended up doing: 好的,谢谢大家,这就是我最终做的事情:

NSString * timeAsString = "12:26:07";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate * dateZero = [dateFormatter dateFromString:@"00:00:00"];
NSDate * dc = [dateFormatter dateFromString:timeAsString];
NSTimeInterval startTime = [dc timeIntervalSinceDate:dateZero];

It is not an elegant solution but it works, at least for what I need to do, 它不是一个优雅的解决方案,但它起作用,至少对我需要做的事情,

You can use NSDateComponent to create dates based on a time. 您可以使用NSDateComponent根据时间创建日期。 You can add values to the year based on the current date or a future/past date. 您可以根据当前日期或未来/过去日期向年份添加值。

NSDateComponents *component=[[NSDateComponents  alloc] init];
[component setHour:yourHour];
[component setMinute:yourMinutes];
[component setYear:yourYear];
[component setMonth:yourMonth];
[component setDay:yourDaty];
NSCalendar *calendar=[NSCalendar currentCalendar];
NSDate *date=[calendar dateFromComponents:component];

NSDate *myDateVariable; NSDate * myDateVariable;

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];    //Not shown
[someDateLabel setText:[dateFormatter stringFromDate:[myDateVariable]]];

//OR FOR BESPOKE FORMATTING
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"'Due' EEEE d MMM yyyy, h:mm a"];
[someDateLabel setText:[dateFormatter stringFromDate:[myDateVariable]]];

//Time using users 24/12 hour preference:
[dateFormatter setDateFormat:@"HH:mm"];

//Time using 12 hour AM/PM format:
[dateFormatter setDateFormat:@"h:mm a"];

//Day and date using abbreviated words:
[dateFormatter setDateFormat:@"' ('EEE d MMM yyyy')'"];

//Day and date using full words:
[dateFormatter setDateFormat:@"' ('EEEE d MMMM yyyy')'"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM