Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2025/11/23 20:31:30.065 (台灣標準時)

Python の datetime モジュールの基本的な使い方

Python で datetime モジュールを使って、現在の日時を得る方法は以下の通 り。


#!/usr/pkg/bin/python3.9

# importing datetime module
import datetime

# getting current date/time
datetime_localtime = datetime.datetime.now ()

# printing current date/time
print ("current time in local time:", datetime_localtime)

# getting current date/time in UTC
datetime_utc = datetime.datetime.now (datetime.timezone.utc)
print ("current time in UTC:       ", datetime_utc)

# YYYY, MM, DD, hh, mm, ss
YYYY = datetime_localtime.year
MM   = datetime_localtime.month
DD   = datetime_localtime.day
hh   = datetime_localtime.hour
mm   = datetime_localtime.minute
ss   = datetime_localtime.second + datetime_localtime.microsecond * 10**-6
print ("YYYY/MM/DD = %04d/%02d/%02d" % (YYYY, MM, DD) )
print ("hh:mm:ss   = %02d:%02d:%09.6f" % (hh, mm, ss) )

実行してみると次のようになる。

% ./test_datetime_0.py
current time in local time: 2021-02-04 13:59:15.191673
current time in UTC:        2021-02-04 05:59:15.191734+00:00
YYYY/MM/DD = 2021/02/04
hh:mm:ss   = 13:59:15.191673


Frequently accessed files

  1. Misc___Taiwan/20240207_00.html
  2. Computer___TeX/20231107_00.html
  3. Computer___TeX/20230726_01.html
  4. Computer___NetBSD/20220506_0.html
  5. Misc___Taiwan/20240903_01.html
  6. Computer___NetBSD/20250307_00.html
  7. Misc___Taiwan/20210215_0.html
  8. Photo___Taiwan/20220711_1.html
  9. Computer___Python/20220715_0.html
  10. Food___Taiwan/20230622_13.html
  11. Food___Taiwan/20230622_25.html
  12. Science___General/20220615_0.html
  13. Computer___NetBSD/20230121_00.html
  14. Travel___Taiwan/20230725_03.html
  15. Computer___NetBSD/20240101_02.html
  16. Computer___TeX/20240411_00.html
  17. Food___Taiwan/20230611_06.html
  18. Computer___Network/20231225_00.html
  19. Computer___Network/20240130_00.html
  20. Misc___Japan/20240718_00.html
  21. Computer___NetBSD/20220422_0.html
  22. Travel___Taiwan/20210225_5.html
  23. Computer___TeX/20240414_00.html
  24. Food___Taiwan/20230610_00.html
  25. Computer___NetBSD/20241223_00.html
  26. Computer___Hardware/20240820_00.html
  27. Computer___NetBSD/20250312_00.html
  28. Computer___NetBSD/20241102_00.html
  29. Food___Taiwan/20230624_15.html
  30. Misc___Taiwan/20240819_00.html


HTML file generated by Kinoshita Daisuke.