0

I have a date in string format, example:- "2017-07-31" or can be multiple dates (any) in string format. My requirement is to check this date to current date and if it is greater than 0 and less than 15, then that time I have to do another operation.

So first I am converting that date string to in date format. But it is giving one day ago date. Here is my code:

//Date from string
func dateFromString(date : String) -> Date {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    let currentDate = (dateFormatter.date(from: date))//(from: date))
    return currentDate!
}

Ex. my date is "2017-08-30" and this function is returning 2017-08-29 18:30:00 +0000 in date format. It means 1 day ago. I am little bit confuse about dates operation. I read so many blogs also.

After that I have to check this date to current date if it is in between 0 < 15 than I will do other operation.

Comparing two dates:

extension Date {
  func daysBetweenDate(toDate: Date) -> Int {
    let components = Calendar.current.dateComponents([.day], from: self, to: toDate)
    return components.day ?? 0
  }
}

If my date is today date and comparing to tomorrow date then also it is giving 0 days difference. Why?

halfer
  • 19,824
  • 17
  • 99
  • 186
Kishor Pahalwani
  • 1,010
  • 1
  • 23
  • 53

3 Answers3

1

If – for example – the current date is 2017-07-31 at 11AM then the difference to 2017-08-01 (midnight) is 0 days and 13 hours, and that's why you get "0 days difference" as result.

What you probably want is to compare the difference between the start of the current day and the other date in days:

extension Date {
    func daysBetween(toDate: Date) -> Int {
        let cal = Calendar.current
        let startOfToday = cal.startOfDay(for: self)
        let startOfOtherDay = cal.startOfDay(for: toDate)
        return cal.dateComponents([.day], from: startOfToday, to: startOfOtherDay).day!
    }
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks to explaining. It is working but I want to ask one thing that is current date will compare "2017-07-31" so there is already 24 hours difference to 2017-08-01 but it is giving 0 day. So u r using startOfDay? – Kishor Pahalwani Jul 31 '17 at 08:03
0

Try this method for convert string to date:

func dateFromString(date : String) -> Date {
  let dateFormatter = DateFormatter()
  dateFormatter.dateFormat = "yyyy-MM-dd"
  dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC")
  let currentDate = (dateFormatter.date(from: date))//(from: date))
  return currentDate!
}
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
0

Try this to compare the time between two dates in seconds :

  var seconds = Calendar.current.dateComponents([.second], from: date1!, to: date2!).second ?? 0

    seconds = abs(seconds)

    let min  = seconds/60     // this gives you the number of minutes between two dates
    let hours = seconds/3600  // this gives you the number of hours between two dates
    let days = seconds/3600*24  // this gives you the number of days between two dates
Samarth Kejriwal
  • 1,168
  • 2
  • 15
  • 30