2016-05-30 29 views
3

時間間隔需要幫助,以顯示每30分鐘的時間間隔,假設current time is 11:45 am然後如何獲得每個30:00(30分鐘),從當前時間至晚上10:30

的時間間隔應該是: 12:00 pm,12:30 pm,01:00 pm,01:30 pm,02:00 pm,02:30 pm......10:30 pm.

NSString *time = @"10.30 pm"; 

    NSDate *date1; 
     NSDate *date2; 
     { 
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
      [formatter setDateFormat:@"hh.mm a"]; 
      date1 = [formatter dateFromString:time]; 
      date2 = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]]; 

     } 
     NSTimeInterval interval = [date1 timeIntervalSinceDate: date2];//[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970]; 
     int hour = interval/3600; 
     int minute = (int)interval % 3600/60; 

     NSLog(@"%@ %dh %dm", interval<[email protected]"-":@"+", ABS(hour), ABS(minute)); 

此代碼返回我的當前時間和給定的時間我如何進一步進行區別。

+0

嘗試使用'NSDateComponents'來操作小時和分鐘。 – Shubhank

+0

檢查我的答案。這是工作perfact作爲您的需要 – Lion

回答

4

你可以做這樣的事情,

NSString *startTime = @"02:00 AM"; 
NSString *endTime = @"11:00 AM"; 


NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; 
[timeFormat setDateFormat:@"hh:mm a"]; 
NSDate* fromTime = [timeFormat dateFromString:startTime]; 
NSDate* toTime = [timeFormat dateFromString:endTime]; 
NSDate *dateByAddingThirtyMinute ; 
NSTimeInterval timeinterval = [toTime timeIntervalSinceDate:fromTime]; 
NSLog(@"time Int %f",timeinterval/3600); 
float numberOfIntervals = timeinterval/3600; 
NSLog(@"Start time %f",numberOfIntervals); 

for(int iCount = 0;iCount < numberOfIntervals*2 ;iCount ++) 
{ 
    dateByAddingThirtyMinute = [fromTime dateByAddingTimeInterval:1800]; 
    fromTime = dateByAddingThirtyMinute; 
    NSString *formattedDateString; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"hh:mm a"]; 
    formattedDateString = [dateFormatter stringFromDate:dateByAddingThirtyMinute]; 
    NSLog(@"Time after 30 min %@",formattedDateString); 
} 

在for循環中我已經採取了numberOfIntervals*2因爲時間間隔爲30 min,所以60/30 = 2,你的datebyAddingThirtyMinute是1800,因爲30 min = 1800秒。如果你想要每隔10分鐘之後的時間,那麼它應該是60/10 = 6,所以它應該是numberOfIntervals*6。而你datebyAddingThirtyMinute應該[fromTime dateByAddingTimeInterval:600];

希望這將有助於:)

+0

感謝您的幫助,我們的代碼像一個魅力工作 –

+0

不客氣.... :) – Lion

0

我會首先檢查給定的開始時間是AM還是PM,在這樣做之後,我會找到距離最近(向上)30m的時間間隔,並簡單地增加30m,並檢查我是否在給定停止停止時間,並且如果不增加計數器。

如果您想跟蹤並返回要打印的列表,您也可以將每個30米間隔添加到列表中。

1

試試這個代碼,它爲你工作

NSDateFormatter *datefrmt = [[NSDateFormatter alloc] init]; 
[datefrmt setDateFormat:@"hh:mm a"]; 
NSDate* startingTime = [datefrmt dateFromString:startTime]; 
NSDate* endingTime = [datefrmt dateFromString:endTime]; 
NSLog(@"Starting time is %@", startingTime); 
NSLog(@"Stop time is %@", endingTime);  
NSDate * addingTimeInterval;  
addingTimeInterval = [startingTime dateByAddingTimeInterval:1800];  
NSLog(@"Required output is %@", addingTimeInterval); 
+0

我給出了開始時間「上午10點30分」和結束時間「下午10點30分」,但在這裏的日誌中顯示的餡餅時間是「2000-01-01 05:00:00 +0000 2016-05-30 12:18:38.021 [1932:56987]停止時間爲2000-01-01 17:00:00 +0000 2016-05-30 12:18:38.022 [1932:56987]所需輸出爲2000-01-01 05:30:00 + 0000' –

0

如果你得到的開始日期爲當前日期和時間,然後結束日期將自動設置30分鐘使用dateByAddingTimeInterval:,看我的例子,

NSDate *startDate = [NSDate date]; 

然後設置結束日期是下面的代碼,

currentdate = startDate; //today 
endingDate = [startDate dateByAddingTimeInterval:(60*60)/2]; // 60*60 is 1 hour. 

這種上述方法AUTOMATICA lly計算當前時間結束時間加30分鐘。

希望它有幫助

2

最可靠的方法(也考慮夏令時更改)是利用NSCalendar日期的運算能力。

根本不推薦使用dateByAddingTimeInterval秒。

NSDate *now = [NSDate date]; 
NSCalendar *cal = [NSCalendar currentCalendar]; 
// get minute and hour from now 
NSDateComponents *nowComponents = [cal components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate: now]; 
NSDate *currentDate; 
// if current minutes is not exactly 0 or 30 get back to the past half hour 
if (nowComponents.minute % 30 != 0) { 
    NSInteger pastHalfHourIndex = nowComponents.minute/30; 
    nowComponents.minute = pastHalfHourIndex * 30; 
    currentDate = [cal nextDateAfterDate:now matchingHour: nowComponents.hour minute: nowComponents.minute second: 0 options: NSCalendarMatchNextTime | NSCalendarSearchBackwards]; 
} else { 
    currentDate = now; 
}  
NSMutableArray<NSDate *>* dates = [NSMutableArray array]; 
// loop and add 30 minutes until the end time (10:30 pm) is reached 
while (nowComponents.minute != 30 || nowComponents.hour != 22) { 
    currentDate = [cal dateByAddingUnit:NSCalendarUnitMinute value: 30 toDate: currentDate options: NSCalendarMatchNextTime]; 
    [dates addObject:currentDate]; 
    nowComponents = [cal components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate: currentDate]; 
} 
NSLog(@"%@", dates);