1

Here is my first question;

import Foundation 

let date1 = Date()
let date2 = Date().addingTimeInterval(3600)

if date1 == date2
{
          print("equals")
}
else if date1 > date2
{
          print("date1 is bigger")
}
else if date1 < date2
{
          print("date2 is bigger")
}

It gives below output if i write print("date1") or print("date2")

2018-09-10 08:56:49 +0000

I would like to write the same example but date1 and date2 must include these 2 properties:

format: "dd.MM.yyyy"

locale: "tr_TR"

Beside this, here is my second question:

let date2 = Date().addingTimeInterval(3600)

As you know, this 3600 value adding an hour. How can I add one day? 24*3600? Is there any shortest way?

Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
mannyCalavera
  • 593
  • 1
  • 4
  • 23
  • 2
    https://stackoverflow.com/a/5067868/1891327 – Shamas S Sep 10 '18 at 09:38
  • 2
    Note that `24*3600` is not always a full day. There're sometimes summer time/winter time, and next day at the same hour could be 23 or 25 hours from current time. Look for `Calendar` to add a day."I would like to write the same example but date1 and date2 must include these 2 properties:" Look for `DateFormatter`. – Larme Sep 10 '18 at 09:39
  • Thanks for these details. – mannyCalavera Sep 10 '18 at 10:23
  • Possible duplicate of [How do i add 1 day to a NSDate?](https://stackoverflow.com/questions/5067785/how-do-i-add-1-day-to-a-nsdate) – Kuldeep Sep 10 '18 at 10:55

3 Answers3

1

Try

extension Date {
  func addDays(_ days: Int) -> Date {
    Calendar.autoupdatingCurrent.date(byAdding: .day, value: days, to: self)!
  }
}
regina_fallangi
  • 2,080
  • 2
  • 18
  • 38
0

Try this one

let today = Date() // OR your date here
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: today)
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: today)
Nikunj Kumbhani
  • 3,758
  • 2
  • 26
  • 51
0

Like @Larme said, you might want to look into Calendar.

var dateComponents = DateComponents()
dateComponents.day = 1
guard let date = Calendar.current.date(byAdding: dateComponents, to: Date()) else {  // Adding date components to current day.
   fatalError("date not found")
}
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short // dd.MM.yyyy
dateFormatter.locale = Locale(identifier: "tr_TR") // Your preferred locale
let dateWithLocale = dateFormatter.string(from: date)
print(date)

Your comparison can be done using the Date objects. Only when you need to print it or use it as a String, you would need to do formatting.

Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
  • Dear, @Rakesha Shastri, I have question: Let's assume, i have this variable: var birthday : Date? = nil Can I set a current date value to the this birtday variable. But in short format. (Not as a string. You know, birthday is in Date type) Thanks. – mannyCalavera Sep 10 '18 at 10:28
  • @mannyCalavera An instance of `Date` does not have a format. Only when it is converted to string, formatting is relevant. Whenever you use dates for display, you always convert them to a string before using it. This is when you do the formatting part with DateFormatter. For comparison, you can use the date objects. For example, let's say you have 2 integers, a and b (1 & 2). During comparison it doesn't matter if it is stored as `Double`, `Float` or `Int` as long as the value isn't changed. But when you display it, you will convert it to a particular format. eg: 1.00 using string formatting. – Rakesha Shastri Sep 10 '18 at 10:33
  • 1
    Understand. Thanks a lot. – mannyCalavera Sep 10 '18 at 10:38
  • 1
    When can we see that fatalError("date not found")? I mean, is there any possibility to see this fatalError scope? It gets the current date and then it adds one day. Which case we see this fatalError scope. I just wonder Dear @Rakesha Shastri – mannyCalavera Sep 10 '18 at 10:56
  • Like link below? https://stackoverflow.com/questions/5067785/how-do-i-add-1-day-to-a-nsdate – mannyCalavera Sep 10 '18 at 10:57
  • @mannyCalavera Hmm... good question. I can't think of one immediately, i just wrapped it up in a guard statement because the date returned by `Calendar` was optional. – Rakesha Shastri Sep 10 '18 at 11:01
  • @mannyCalavera i just tried subtracting 100000000 from the current date and it still returned a "\n" as a value for date. So i guess it is something like `UITextField`'s text property. – Rakesha Shastri Sep 10 '18 at 11:07
  • Thanks @Rakesha Shastri – mannyCalavera Sep 10 '18 at 13:15