ios - How to make 30 minute time intervals in swift 2.3 -


starttime = 10:00 endtime = 01:00 pm

now want split time in interval of 30mins like

10:00 10:30 11:00 11:30 .......... till 01:00 pm.

i tried like

   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) nsdate?)!             let totime:nsdate  = (timeformat.date(from:endtime) nsdate?)!              var datebyaddingthirtyminute : nsdate!             let timeinterval : timeinterval = totime.timeintervalsince(fromtime date)             let numberofintervals : double = timeinterval / 3600;             var formatteddatestring : string!               _ 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! date) string?                 print("time after 30 min = \(formatteddatestring)")              }              return formatteddatestring          } 

tried things , got 10:10,10:40..etc how make 30 min round of interval 10:00,10:30...etc

thanks in advance

use below function if user enters anytime less 30 start next 30 min e.g 10:20, start 10:30. , if user give time greater 30 time 00 e.g 10:45, start 11:00.

func maketimeinterval(starttime:string ,endtime:string) -> string {     var arr = starttime.components(separatedby: " ")[0].components(separatedby: ":")     let str = arr[1] 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) nsdate?)!     let totime:nsdate  = (timeformat.date(from:endtime) nsdate?)!      var datebyaddingthirtyminute : nsdate!     let timeinterval : timeinterval = totime.timeintervalsince(fromtime date)     let numberofintervals : double = timeinterval / 3600;     var formatteddatestring : string!       _ 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! date) string?         print("time after 30 min = \(formatteddatestring)")      }      return formatteddatestring  } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -