Skip to content

Instantly share code, notes, and snippets.

@evanre
Created February 9, 2018 12:19
Show Gist options
  • Save evanre/a6d3a959be72621c3177efcd66199bf7 to your computer and use it in GitHub Desktop.
Save evanre/a6d3a959be72621c3177efcd66199bf7 to your computer and use it in GitHub Desktop.
random date and time in nunjucks
{%- macro zeroCheck(num=0) -%}
{{- '0'+num if num < 9 else num -}}
{%- endmacro -%}
{%- macro checkDefault(num=0, rand=1) -%}
{{- num if num else range(1, rand) | random -}}
{%- endmacro -%}
{%- macro randTime(hour='', minute='') -%}
{{- zeroCheck(checkDefault(hour, 24)) -}}:{{- zeroCheck(checkDefault(minute, 59)) -}}
{%- endmacro -%}
{%- macro randDate(day='', month='', year='2018') -%}
{{- zeroCheck(checkDefault(day, 28)) }}-{{ zeroCheck(checkDefault(month, 12)) }}-{{ zeroCheck(checkDefault(year, 2019)) -}}
{%- endmacro -%}
<p>{{randDate()}} at {{randTime()}}</p>
@evanre
Copy link
Author

evanre commented Feb 9, 2018

Sandbox for nunjucks is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment