2016-11-10 56 views
-2

開始時間= 10:00 AM ENDTIME =下午1點00如何時30個分鐘計時間隔SWIFT 2.3

現在我想在30分鐘的間隔分割的時間像

10:00 AM 10:30 AM 11:00 AM 11:30 AM .......... until 01:00 PM。

我試着像

let startDate : NSDate! = NSDate() 
    let time1 : NSDate = startDate.dateByAddingTimeInterval((60*60)/2) 
    let time2 : NSDate = time1.dateByAddingTimeInterval((60*60)/2) 
    let time3 : NSDate = time2.dateByAddingTimeInterval((60*60)/2) 
    let time4 : NSDate = time3.dateByAddingTimeInterval((60*60)/2) 
func makeTimeInterval(startTime:String ,endTime:String) -> String 
     { 

      let timeFormat = DateFormatter() 
      timeFormat.dateFormat = "hh:mm a" 
      var fromTime:NSDate = (timeFormat.date(from:startTime) as NSDate?)! 
      let toTime:NSDate = (timeFormat.date(from:endTime) as NSDate?)! 

      var dateByAddingThirtyMinute : NSDate! 
      let timeinterval : TimeInterval = toTime.timeIntervalSince(fromTime as Date) 
      let numberOfIntervals : Double = timeinterval/3600; 
      var formattedDateString : String! 


      for _ in stride(from: 0, to: Int(numberOfIntervals * 2), by: 1) 
      { 
       dateByAddingThirtyMinute = fromTime.addingTimeInterval(1800) 
       fromTime = dateByAddingThirtyMinute 
       let dateFormatter = DateFormatter() 
       dateFormatter.dateFormat = "hh:mm a" 
       formattedDateString = dateFormatter.string(from: dateByAddingThirtyMinute! as Date) as String? 
       print("Time after 30 min = \(formattedDateString)") 

      } 

      return formattedDateString 

     } 

想這些事情,我得到了像10:10,10:40..etc 如何讓30分鐘一輪像10間隔: 00,10:30 ......等

在此先感謝

+0

爲什麼減號?你能解釋一下嗎? – iPhone25

回答

2

如果用戶進入隨時隨地小於30使用下面的功能比它會與第二天30分鐘如10:20,開始啓動從10:30開始。如果用戶給出超過30的時間,則非常時間將是00,例如10:45,從11:00開始。

func makeTimeInterval(startTime:String ,endTime:String) -> String 
{ 
    var arr = startTime.components(separatedBy: " ")[0].components(separatedBy: ":") 
    let str = arr[1] as String 
    if (Int(str)! > 0 && Int(str)! < 30){ 
     arr[1] = "00" 
    } 
    else if(Int(str)! > 30){ 
     arr[1] = "30" 
    } 
    let startT:String = "\(arr.joined(separator: ":")) \(startTime.components(separatedBy: " ")[1])" 

    let timeFormat = DateFormatter() 
    timeFormat.dateFormat = "hh:mm a" 
    var fromTime:NSDate = (timeFormat.date(from:startT) as NSDate?)! 
    let toTime:NSDate = (timeFormat.date(from:endTime) as NSDate?)! 

    var dateByAddingThirtyMinute : NSDate! 
    let timeinterval : TimeInterval = toTime.timeIntervalSince(fromTime as Date) 
    let numberOfIntervals : Double = timeinterval/3600; 
    var formattedDateString : String! 


    for _ in stride(from: 0, to: Int(numberOfIntervals * 2), by: 1) 
    { 
     dateByAddingThirtyMinute = fromTime.addingTimeInterval(1800) 
     fromTime = dateByAddingThirtyMinute 
     let dateFormatter = DateFormatter() 
     dateFormatter.dateFormat = "hh:mm a" 
     formattedDateString = dateFormatter.string(from: dateByAddingThirtyMinute! as Date) as String? 
     print("Time after 30 min = \(formattedDateString)") 

    } 

    return formattedDateString 

} 
0

您可以使用NSDateComponents班。這個類允許獲得一些日期單位。您可以獲得日期和分鐘數值,並將分鐘舍入到最接近的「良好」值(有時您應該也是更改小時數)。
之後,您可以使用小時和分鐘的新值獲取新的日期。 不要忘記時區。

 NSDate* date = [NSDate date]; //start date 
     NSCalendar* calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]; 
     NSDateComponents* components = [calendar components:(NSCalendarUnitMinute | NSCalendarUnitHour) fromDate:date]; 
     NSInteger minutes = [components minute]; 
     NSInteger hours = [components hour]; 

     NSInteger nearestGoodMinutes = [self _nearestGoodMinutesForMinutes:minutes]; 
     BOOL shouldIncHours = [self _shouldINcHoursForMinutes:minutes]; 
     if (shouldIncHours) 
     { 
      hours++; 
     } 
     NSDate* dateWithGoodHours = [calendar dateBySettingUnit:NSCalendarUnitHour value:hours ofDate:date options:kNilOptions]; 
     NSDate* goodDate = [calendar dateBySettingUnit:NSCalendarUnitMinute value:nearestGoodMinutes ofDate:dateWithGoodHours options:kNilOptions]; 

一些解釋。 NSDate不包含任何日期組件。日期組件取決於當前callendar。組件的值也取決於當前時區。如果您使用標識符創建NSCalendar,您將獲得當前的日曆time zone。時區應與日期中用於displaying的時區相同。

P.S.
只能在開始日期進行。

+0

你有沒有任何示例代碼 – iPhone25

+0

我不使用swift。我可以添加Objective-C代碼,它是簡單的代碼。 –

+0

我已經更新了我的答案 –

0

可以用戶Calendar Components設置分00,然後計算區間:

func getIntervals(start: String, end: String)->[Date]{ 
    let formatter = DateFormatter() 
    formatter.dateFormat = "hh:mm a" 

    var fromDate = formatter.date(from: start)! 
    let calendar = Calendar(identifier: .gregorian) 
    let component = calendar.dateComponents([.hour, .minute], from: fromDate) 
    fromDate = calendar.date(bySettingHour: component.hour!, minute: 0, second: 0, of: fromDate)! 

    let thirtyMin: TimeInterval = 1800 
    let endDate = formatter.date(from: end)! 
    var intervals = Int(endDate.timeIntervalSince(fromDate)/thirtyMin) 
    intervals = intervals < 0 ? 0 : intervals 

    var dates = [Date]() 

    for x in 0...intervals{ 
     let date = fromDate.addingTimeInterval(TimeInterval(x)*thirtyMin) 
     dates.append(date) 
    } 
    return dates 
} 

let timeIntervals = getIntervals(start: "10:10 am", end: "1:40 pm")