LoginSignup
0
1

More than 3 years have passed since last update.

[Django]テンプレートファイル内で日付の表記を指定する

Posted at

サイト上で時刻を表示しようと思った時に表示される形式がちょっと気に食わなかった

例えば

def sample(request):
    now_time = datetime.datetime.now()
    params = {
        'now_time': now_time,
    }
    return render(request, 'sample.html',params)

こんな感じでsample.htmlに時刻などを送ります

sample.html内で

{{ now_time }}

と書くと

2019年9月16日12:36

と表示されます
これ以外の形式で表示させたいことも多いと思うので今回はこれを任意の形式に変えます

以下のように書くと

{{ now_time| date:"Yねん nガツ j日 a g時" }}
2019ねん 9ガツ 16日 p.m. 12時

と表示されます。べんり

https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#date
各文字の機能などはこのページに書かれています。結構種類が多いので日時の表示は自由自在にできそう

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1