0

In Swift 3 I am not able to change the day by adding minutes to a day. The time changes like its supposed to but the day does not change, for example adding 180 minutes to 31.10.2016 2200 leads to 31.10.2016 0100 and not to 01.11.2016 0100.

func getDayByAddingMinutes(date: String, minutes: Int) -> String{
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd.MM.yyyy hh:mm"
    let calendar = Calendar.current
    let akt = (dateFormatter.date(from: date)! as Date?)!
    let calculatedDate = calendar.date(byAdding: .minute, value: minutes, to: akt )
    print("\(akt) +  \(minutes)")
    let dateFormatter2 = DateFormatter()
    dateFormatter2.dateFormat = "dd.MM.yyyy"
    let newDate = dateFormatter2.string(from: calculatedDate!);
    print("\(date) +  \(newDate)")
    return "\(newDate)";
}
Dave
  • 95
  • 1
  • 10

2 Answers2

0

try this:

func getDayByAddingMinutes(date: String, minutes: Int) -> String{
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd.MM.yyyy HH:mm" 
    let calendar = Calendar.current
    let akt = (dateFormatter.date(from: date)! as Date?)!
    let calculatedDate = calendar.date(byAdding: .minute, value: minutes, to: akt )
    print("\(akt) +  \(minutes)")
    let dateFormatter2 = DateFormatter()
    dateFormatter2.dateFormat = "dd.MM.yyyy"
    let newDate = dateFormatter.string(from: calculatedDate!);
    print("\(date) +  \(newDate)")
    return "\(newDate)";
}

You have used hh instead of HH.

Hope this helps you :)

  • unfortunately does not fix it :( – Dave Nov 24 '16 at 19:42
  • Do you have as a date string this format? **31.10.2016 2200** ? If yes, you need to use **dateFormatter.dateFormat = "dd.MM.yyyy HHmm"** – entwicklerfuchs Nov 24 '16 at 19:52
  • the parameter date looks like dd.MM.yyyy HH:mm But the print statement in line 7 looks like that: 2016-12-11 19:01:00 +0000 + 348 The time does not conform in this output, maybe because of the timezone, but the calculated HH:mm fits to the added minutes. Idk what +0000 means, maybe this is causing the issue? – Dave Nov 24 '16 at 20:10
  • +0000 means zulu time. That could be your issue. Unfortunately I am not sure how to overcome that hurdle. Afaik you need to set the locale to posix for both dareformatter. – entwicklerfuchs Nov 24 '16 at 20:28
  • Two questions: 1. What will be printed? **print( Date() )** 2. did you already tried this? instead of calendar, use **let calculatedData = akt.addingTimeInterval( 60 * minutes)** – entwicklerfuchs Nov 24 '16 at 22:01
0
let minutes = 1000
let calendar = Calendar.current
let day = "23.01.2017"
let time = "21:15"
let inFormatter = DateFormatter()
inFormatter.locale = Locale(identifier: "en_US_POSIX")
inFormatter.dateFormat = "dd.MM.yyyy HH:mm"
let d2 = inFormatter.date(from: "\(day) \(time)")!
let calculated = calendar.date(byAdding: .minute, value: minutes, to: d2)
let outFormatter = DateFormatter()
outFormatter.locale = Locale(identifier: "en_US_POSIX")
outFormatter.dateFormat = "dd.MM.yyyy"
var date = outFormatter.string(from: calculated!)
Dave
  • 95
  • 1
  • 10