4

My problem is I want to have some way to represent time (without date), like time of day in my iOS app. 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]

So my questions are 1- Is NSTimeInterval (instead of NSDate) what I should be using to store time of day?
2- How can I convert "03:04:05" to and objective-c object from one of the built in frameworks.

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.

Ali
  • 18,665
  • 21
  • 103
  • 138
  • hope this can be helpful. this post shows how you can have a string with date [1]: http://stackoverflow.com/questions/936969/nsdate-and-nsdateformatter-short-format-date-and-time-in-iphone-sdk – Pitono Sep 27 '12 at 14:58
  • If it's just a time of day it doesn't seem like it's meaningful to store it as or have it represent a date. What is the value used for? How is it presented to the user? – Carl Veazey Sep 27 '12 at 14:59
  • Let's say I wan't to have a countdown timer than starts or ends at a specific time (not date involved, happens everyday). There are plenty of use cases for time independent the date, do you know why there is no `NSTime`? – Ali Sep 27 '12 at 15:02
  • 1
    Ah, so it's just a time to represent a whole set of dates then. That makes sense. I don't think `NSTime` would be that useful of a class to be honest, `NSDate` and `NSDateComponents` take care of what simple strings and numbers won't. Use J2theC's solution, every day creating a new date for your countdown. – Carl Veazey Sep 27 '12 at 15:12

3 Answers3

6

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,

Ali
  • 18,665
  • 21
  • 103
  • 138
4

You can use NSDateComponent to create dates based on a time. 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];
J2theC
  • 4,412
  • 1
  • 12
  • 14
0

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')'"];
bicanul123
  • 427
  • 7
  • 21