Where do I configure date formatting for a card/entity

I want to add the uptime for my router to a card. I can’t get it to display in a sensible format like “15 days, 4 hours” or something like that. How can I do that?

It currently displays “2 weeks ago” which could be interpreted as “when were you powered on last?” but not as “how long have you been running?”.

Either way, date formatting seems to be… missing? I can’t find it anywhere. Same for number formatting I guess, whenever it’ll come to that.

I did find this absolute unit of a post:

Which is very good for datetime-manipulations, but it doesn’t explain what I do with any of that code. Novice user here, to be clear. I’m not afraid of code, but I don’t know where to put it.

Edit: Found this:

Apparently there’s a format field. Would be nice to have some intellisense for better discoverability I suppose, right? But nevermind, this field is the closest to what I want, so I guess “X days, Y hours” is simply not possible, correct?

You put it into a template sensor.

here is an example that you can use to modify for your use.

it gives the HA uptime in a human readable for format from a datetime like you want:

sensor:
  - platform: template
    sensors:
      homeassistant_uptime:
        friendly_name: HomeAssistant Uptime
        value_template: >-      
          {%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.ha_uptime')) %}
          {%- macro phrase(name, divisor, mod=None) %}
            {%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
            {%- set end = 's' if value > 1 else '' %}
            {{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
          {%- endmacro %}
          {%- set values = [ 
                     phrase('wk', 60*60*24*7), 
                     phrase('day', 60*60*24, 7), 
                     phrase('hr', 60*60, 24),
                     phrase('min', 60)
                 ] | select('!=','') | list %}
          {{ values[:-1] | join(', ') ~ ' and ' ~ values[-1] if values | length > 1 else values | first | default(none)}}

just change the base sensor (sensor.ha_uptime) to your entity that gives you a datetime.

So I plonk that code into that template editor, and change the entity ID. Got it.

And then what?

did you read the docs I linked for creating a template sensor?

You add it to your system like any other sensor entity. And how you do that depends on how you add stuff to your config. Either manually via yaml to the configuration.yaml or via the UI by adding a template ‘helper’.

Once you add that code I gave you it will create a sensor in your system that you can display in a card just like any other entity.