user1999892
user1999892

Reputation: 135

I want time in HH:mm format from string in cocoa

I want to get two time values, which are in string format like this:

eg: NSString *fromTime=@"11:30"; NSString *toTime=@"12:30";

I want that in time format, i tried using NSDateFormatter,

   `NSDateFormatter *df=[[NSDateFormatter alloc] init];
   [df setDateFormat:@"HH:mm"];
    NSDate *from=[df dateFromString:fromTime];
    NSLog(@"from %@",from);
    NSDate *to=[df dateFromString:toTime];
    NSLog(@"to %@",to);`

Output: from 1970-01-01 06:00:00 +0000 to 1970-01-01 07:00:00 +0000

But i want only the time.

Upvotes: 0

Views: 651

Answers (2)

NewStack
NewStack

Reputation: 990

you can try this method stringFromDate

         NSDateFormatter *df=[[NSDateFormatter alloc] init];
        [df setDateFormat:@"HH:mm"];
        NSDate *from=[df dateFromString:fromTime];
        NSLog(@"from %@",[df stringFromDate:from]);
        NSDate *to=[df dateFromString:toTime];
        NSLog(@"to %@",[df stringFromDate:to]);

Upvotes: 0

rckoenes
rckoenes

Reputation: 69469

A NSDate is a full date object, it will always contain a full date and time. It will also return a date with GMT as the timezone (+0000 part after the time).

You can just ignore the date part and just use the time part.

Upvotes: 1

Related Questions