開始時間= 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 ......等
在此先感謝
爲什麼減號?你能解釋一下嗎? – iPhone25