<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 如何在 Python 中獲取當前日期和時間? > 原文: [https://www.programiz.com/python-programming/datetime/current-datetime](https://www.programiz.com/python-programming/datetime/current-datetime) #### 在本文中,您將學習使用 Python 獲取今天的日期以及當前的日期和時間。 我們還將使用`strftime()`方法將日期和時間格式化為不同的格式。 您可以采用多種方法來獲取當前日期。 我們將使用[日期時間](/python-programming/datetime)模塊的`date`類來完成此任務。 * * * ## 示例 1:Python 獲取今天的日期 ```py from datetime import date today = date.today() print("Today's date:", today) ``` 在這里,我們從`datetime`模塊導入了`date`類。 然后,我們使用`date.today()`方法獲取當前的本地日期。 順便說一句,`date.today()`返回一個`date`對象,該對象在上述程序中分配給了`today`變量。 現在,您可以使用[`strftime()`](/python-programming/datetime/strftime)方法創建一個以不同格式表示日期的字符串。 * * * ## 示例 2:格式不同的當前日期 ```py from datetime import date today = date.today() # dd/mm/YY d1 = today.strftime("%d/%m/%Y") print("d1 =", d1) # Textual month, day and year d2 = today.strftime("%B %d, %Y") print("d2 =", d2) # mm/dd/y d3 = today.strftime("%m/%d/%y") print("d3 =", d3) # Month abbreviation, day and year d4 = today.strftime("%b-%d-%Y") print("d4 =", d4) ``` 當您運行程序時,輸出將類似于: ```py d1 = 16/09/2019 d2 = September 16, 2019 d3 = 09/16/19 d4 = Sep-16-2019 ``` * * * 如果需要獲取當前日期和時間,可以使用`datetime`模塊的`datetime`類。 ## 示例 3:獲取當前日期和時間 ```py from datetime import datetime # datetime object containing current date and time now = datetime.now() print("now =", now) # dd/mm/YY H:M:S dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print("date and time =", dt_string) ``` 在這里,我們使用`datetime.now()`來獲取當前日期和時間。 然后,我們使用`strftime()`創建一個以另一種格式表示日期和時間的字符串。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看