开发者

Splitting time to 30 mins interval in iphone

I have 2 NSString say startTime and endTime.

startTime = 10:00 AM

endTime = 7:00 PM

Now i want to split the time in the interval of 30mins, so i can get time like this:

10:00 AM 10:30 AM 11:00 AM 11:30 AM .......... till 7:00 PM.

i'm trying something like this :

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[开发者_如何学PythontimeFormat setDateFormat:@"HH:mm"];
NSDate* fromTime = [timeFormat dateFromString:startTime];
NSDate* toTime = [timeFormat dateFromString:endTime];

But i'm getting the from fromTime = 1970-01-01 6:00:00 +0000 for the value of startTime = 11:30

Anybody has any idea,how to do it???



NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"hh:mm a"];
NSDate* fromTime = [timeFormat dateFromString:startTime];
NSDate* toTime = [timeFormat dateFromString:endTime];

NSLog(@"Start time %@",fromTime);
NSLog(@"End time %@",toTime);

NSDate *dateByAddingThirtyMinute;

dateByAddingThirtyMinute = [fromTime dateByAddingTimeInterval:1800];

NSLog(@"Time after 30 min %@",dateByAddingThirtyMinute);

Try the above code.


It may just be that you're not giving enough information when using dateFromString. If you're only giving the hh:mm, the rest of the date could cause several issues for you.

If you only need to show intervals of 30 minutes, and it's simply between two times in the same day (and dates don't matter), it would probably be wise to avoid dates altogether and just manually calculate the intervals between the two. You could easily parse the start time and end times into an hours and minutes integers, and then go from there.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜