Notebook

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

HTML ファイル生成日時: 2025/08/15 15:56:28.294 (台灣標準時)

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. Misc___Taiwan/20240819_00.html
  3. Computer___NetBSD/20230119_00.html
  4. Computer___TeX/20231107_00.html
  5. Computer___Network/20230516_00.html
  6. Misc___Taiwan/20240903_01.html
  7. Computer___Network/20230726_00.html
  8. Computer___NetBSD/20250301_01.html
  9. Computer___Network/20241214_00.html
  10. Computer___NetBSD/20240805_03.html
  11. Computer___Network/20240130_00.html
  12. Computer___TeX/20240414_00.html
  13. Food___Taiwan/20230609_06.html
  14. Computer___TeX/20230726_01.html
  15. Misc___Taiwan/20250728_01.html
  16. Food___Taiwan/20230608_04.html
  17. Computer___TeX/20240414_01.html
  18. Computer___Python/20240101_00.html
  19. Computer___FreeBSD/20220621_0.html
  20. Misc___Japan/20240610_00.html
  21. Computer___NetBSD/20250409_00.html
  22. Computer___NetBSD/20241102_00.html
  23. Science___Math/20220420_0.html
  24. Computer___TeX/20240410_00.html
  25. Food___Taiwan/20230711_15.html
  26. Computer___TeX/20240411_00.html
  27. Computer___NetBSD/20240810_00.html
  28. Computer___NetBSD/20250728_00.html
  29. Computer___Network/20230508_00.html
  30. Misc___Taiwan/20220831_4.html


HTML file generated by Kinoshita Daisuke.