Python 程式顯示了不同的日期時間格式


datetime 模組提供了處理日期和時間所要用到的類。我們將展示不同的格式,如星期、星期幾、天數等。

演算法

Step 1: Import datetime.
Step 2: Print day of the week.
Step 3: Print week number.
Step 4: Print day of the year.

示例程式碼

即時演示

import datetime

print("Day of the week: ", datetime.date.today().strftime("%A"))
print("Week number: ", datetime.date.today().strftime("%W"))
print("Day of the year: ", datetime.date.today().strftime("%j"))

輸出

Day of the week:  Sunday
Week number:  06
Day of the year:  045

說明

strftime() 函式的引數說明如下

  • %A:完整的星期名稱(例如:“星期一”)
  • %W:一年的週數,星期天為一週的第一天
  • %j:一年中的第幾天,以零填充的十進位制數字表示

更新於: 2021 年 3 月 16 日

234 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.