2

I'd like to get a string of format "m:ss" out of two dates. E.g.: "0:27" for 27 seconds difference and "1:30" for 90 seconds difference between dates.

Here's the code I'm using:

import Foundation
let formatter = DateIntervalFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .none
formatter.dateTemplate = "m:ss"

let startDate = Date()
let endDate = Date(timeInterval: 1, since: startDate)

let outputString = formatter.string(from: startDate, to: endDate)
print(outputString) //16:12 – 16:13 ???
// This is correct, but it doesn't actually calculate the interval.

But I'm getting just two dates printed out with a dash. How can I actually make the DateIntervalFormatter to calculate the difference as I want?

The code is almost 1:1 sample from the Apple documentation but with the custom dateTemplate: https://developer.apple.com/documentation/foundation/dateintervalformatter

Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
  • But this is the same result as in the linked page, isn't it? – Joakim Danielson Nov 18 '21 at 16:29
  • Please, take a look at the `dateTemplate` property and that I set `dateStyle` and `timeStyle` to `none`. The `DateIntervalFormatter` uses the correct template, but doesn't calculate the difference. Is it even possible to use this formatter to get a difference between the two dates or should I do this manually instead? Any options for the formatter to not to provide a string of format `A-B` where A & B are dates? – Richard Topchii Nov 18 '21 at 16:33
  • 1
    That you are setting the style properties to .none doesn’t really matter since they are not used when you set the dateTemplate property. What I meant is that the example doesn’t calculate a difference so why should your code do it? – Joakim Danielson Nov 18 '21 at 16:51
  • I thought that I could make it somehow calculate the difference, as the class name and description imply that. E.g. the interval between today and a week ago is "A week" or "7 days" etc. It's a time interval – Richard Topchii Nov 18 '21 at 19:19

2 Answers2

2

It seems that you actually want DateComponentsFormatter

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.minute, .second]
formatter.zeroFormattingBehavior = .pad
let startDate = Date()
let endDate = Date(timeInterval: 129, since: startDate)

let outputString = formatter.string(from: startDate, to: endDate)!
print(outputString)

to remove the leading zero if the minutes are < 10 you could use Regular Expression

print(outputString.replacingOccurrences(of: "^0(\\d)", with: "$1", options: .regularExpression))
vadian
  • 274,689
  • 30
  • 353
  • 361
0

I created this solution which doesn't involve the DateIntervalFormatter:

import Foundation

let minutes = 2
let seconds = 9

let formatted = String(format: "%01d:%02d", minutes, seconds)
print(formatted) // 2:09

Looks like what DateIntervalFormatter does is just applying a standard Date->String conversion to both of the dates and adds a dash between them.

Richard Topchii
  • 7,075
  • 8
  • 48
  • 115