10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pendulumとは

日時の操作を容易にするPythonパッケージ
https://pendulum.eustace.io/

前回

Pendulum使って簡単日時実装①

format()

localeで設定した言語に変換され表示される
(デフォルトは英語)

import pendulum

dt = pendulum.datetime(1975, 5, 21)
dt.format('dddd DD MMMM YYYY')
# 'Wednesday 21 May 1975'

dt.format('dddd DD MMMM YYYY', locale='de')
# 'Mittwoch 21 Mai 1975'

diff_for_humans()

デフォルトは英語なのでpendulum.set_locale()を使用して言語を設定

print(pendulum.now().add(years=1).diff_for_humans())
# in 1 year

pendulum.set_locale('ja')
print(pendulum.now().add(years=1).diff_for_humans())
# 1 年後

上記のコードだとローカルが変わってしまう。

ローカル変更を避けるための記述法
dt = pendulum.now().add(years=1)
dt.diff_for_humans(locale='fr')
# 'dans 1 an'
# localはjaのまま

最後に

認識間違っていればご指摘お願い致します。

10
3
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
10
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?