DateComponentsFormatter to get time as in format of 1y 1m 1d 2h 1min in swift

297 Views Asked by At
func calculateTimeUsingFormatter(seconds : Int) -> String
{
    let formatter = DateComponentsFormatter()
    formatter.unitsStyle = .abbreviated
    formatter.zeroFormattingBehavior = .pad
    formatter.allowedUnits = [.year, .month , .day, .hour , .minute]

    let minutes1 = seconds / 60
    let timeInterval = TimeInterval(minutes1 * 60)
    return formatter.string(from: timeInterval)!
}

I gave input as:

24 * 395 * 60 * 60 + 48 * 60 * 60 + 60 + 60 * 60 + 31 + 60 * 60 

then I got an output as:

1y 1m 1d 2h 1m 

but I need an actual output as:

1y 1m 1d 2h 1min 

Could you please help me?

0

There are 0 best solutions below