-1

how can I display date iterval in years (or years + months) using Foundation’s Measurements()? I’ve made it to display interval but biggest unit I’ve found it hours … and it is not valid for me (display with beautiful localisation 87600 hours for 10y interval)

how to fix this?

my code:

// MODEL part
var yearsLived: Measurement<UnitDuration>? {
    if let hours = birthDate?.getInterval(toDate: deathDate, component: .hour) {
        let hours = Measurement(value: Double(hours), unit: UnitDuration.hours)
        return hours
    }
    return nil
}

// VC PART:
if let yearsLived = person?.yearsLived {
    let formatter = MeasurementFormatter()
    formatter.unitStyle = .long
    formatter.unitOptions = [.naturalScale]

    yearsLivedLabel.text = formatter.string(from: yearsLived)
}
Paweł Madej
  • 1,229
  • 23
  • 42
  • 1
    Measurement is not how you display time intervals. – matt Jun 18 '19 at 11:52
  • so what it is Unit Duration else like time interval? from documentation: Duration Duration is a quantity of time. The SI unit for time is the second (sec), which is defined in terms of the radioactivity of a cesium-133 atom. Duration is also commonly expressed in terms of minutes (min) and hours (hr). years / months / days is just another time interval expression – Paweł Madej Jun 18 '19 at 11:57
  • It also say in the documentation to use DateComponents for years, months, weeks and days. – Joakim Danielson Jun 18 '19 at 12:04
  • I look for solution not argueing. – Paweł Madej Jun 18 '19 at 12:44

1 Answers1

1

how can I display date iterval in years (or years + months) using Foundation’s Measurements

You don't. You use DateComponentsFormatter.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • thank you @matt that worked. I'm sorry about little misunderstanding whether to use Measurements or DateComponents for proper localization strings. Now it works perfectly with your help – Paweł Madej Jun 18 '19 at 13:09