Get an upcoming date between two date iOS swift
Problem:
I had two date with me fromdate and todate.I want to get the upcoming date(greater than today date).
Solution:
func getUpcomingDate(fromDate: NSDate, toDate: NSDate)-> String{
var fromDatet = fromDate
repeat {
if fromDatet.isGreaterThanDate(dateToCompare: NSDate()){
return dateStr
}
fromDatet = fromDatet.addDays(daysToAdd: 1)
} while (fromDatet.isLessThanDate(dateToCompare: toDate) || fromDatet.isEqual(to: toDate as Date))
return ""
}
//adding the days to the date
func addDays(daysToAdd: Int) -> NSDate {
let secondsInDays: TimeInterval = Double(daysToAdd) * 60 * 60 * 24
let dateWithDaysAdded: NSDate = self.addingTimeInterval(secondsInDays)
//Return Result
return dateWithDaysAdded
}
//check if less than date
func isLessThanDate(dateToCompare: NSDate) -> Bool {
//Declare Variables
var isLess = false
//Compare Values
if NSDate().compare(dateToCompare as Date) == ComparisonResult.orderedAscending {
isLess = true
}
//Return Result
return isLess
}
I had two date with me fromdate and todate.I want to get the upcoming date(greater than today date).
Solution:
func getUpcomingDate(fromDate: NSDate, toDate: NSDate)-> String{
var fromDatet = fromDate
repeat {
if fromDatet.isGreaterThanDate(dateToCompare: NSDate()){
return dateStr
}
fromDatet = fromDatet.addDays(daysToAdd: 1)
} while (fromDatet.isLessThanDate(dateToCompare: toDate) || fromDatet.isEqual(to: toDate as Date))
return ""
}
//adding the days to the date
func addDays(daysToAdd: Int) -> NSDate {
let secondsInDays: TimeInterval = Double(daysToAdd) * 60 * 60 * 24
let dateWithDaysAdded: NSDate = self.addingTimeInterval(secondsInDays)
//Return Result
return dateWithDaysAdded
}
//check if less than date
func isLessThanDate(dateToCompare: NSDate) -> Bool {
//Declare Variables
var isLess = false
//Compare Values
if NSDate().compare(dateToCompare as Date) == ComparisonResult.orderedAscending {
isLess = true
}
//Return Result
return isLess
}
Comments
Post a Comment