12-digit timestamp for "Readwise to Obsidian"

For those of you that use the “Readwise to Obsidian” plugin, and want to have the original Zettlekasten like IDs (The default format is 12-digit timestamp, e.g. “202001010000”) for the notes it generates, I wrote some Jinja code to make it happen:

copypasta:

{{date|date("Ymd")}}{% for char in time|replace(":", "")|replace("am", "")|replace("pm", "")|list %}{% if loop.index == 1 and time[1] == ":" and "am" in time %}0{{char}}{% elif loop.index < 3 and time[2] == ":" and time[1] == "2" and "am" in time %}0{% elif loop.index == 1 and time[1] == ":" and "pm" in time %}{{char|int() + 12}}{% elif loop.index < 3 and time[2] == ":" and "pm" in time %}{{char|int() + loop.index}}{% else %}{{char}}{% endif %}{% endfor %}

formatted: (doesn’t work as a copypasta but lets you see what it does)

{{date|date("Ymd")}}
{% for char in time|replace(":", "")|replace("am", "")|replace("pm", "")|list %}
  <!-- means hour is 1-9am -->
  {% if loop.index == 1 and time[1] == ":" and "am" in time %}
    0{{char}}
  <!-- means 12am -->
  {% elif loop.index < 3 and time[2] == ":" and time[1] == "2" and "am" in time %}
    0
  <!-- means hour is 1-9pm -->
  {% elif loop.index == 1 and time[1] == ":" and "pm" in time %}
    {{char|int() + 12}}
  <!-- means hour is 10-12pm -->
  {% elif loop.index < 3 and time[2] == ":" and "pm" in time %}
    {{char|int() + loop.index}}
  {% else %}
    {{char}}
  {% endif %}
{% endfor %}
1 Like