Python 中如何获取当前日期?

在本节中,我们将了解如何使用各种方法检索 Python 的当前日期。日期是应用或网站或数据库服务器的重要组成部分,它指示软件创建的日期和时间、存储的网站记录、应用版本等。例如,当用户使用应用或网站进行某些工作时,软件会存储具有特定日期和时间的用户信息。此外,我们可以说当我们访问任何社交媒体网站,如推特、脸书、WhatsApp 等。它们存储用户的信息,特定用户的帖子、分享或发送消息的日期等。因此,我们可以定义日期和时间在软件应用中的重要性。

在 Python 编程语言中,datetime 模块包含一个用于访问计算机当前日期和时间的 datetime 类。

使用日期时间. now()方法

Python datetime.now()方法,用于显示系统的当前日期和时间。它定义了 Python 库的日期时间模块中的函数。

语法:


datetime.now()

考虑一个使用 Python 中的 datetime.now()方法访问系统当前日期和时间的程序。

program.py


import datetime 
dt = datetime.datetime.now() # use now() method in datetime
print( "Display the current date of the system: ") # current date 
print (str (dt) ) # call the dt variable to print the system date.

输出:

Display the current date of the system:
2021-02-28 18:56:52.799555

使用 now()方法

now()方法: Python now()方法用于显示系统当前日期。

语法:


now() # fetch the current date 

考虑一个程序,使用 Python 中的 now()方法访问系统的当前日期和时间。

Program.py


# import the datetime class or module from the datetime module.
from datetime import datetime
now = datetime.now() # Use now() to access the current date and time 
print("Current date and time is ", now)

输出:

Current date and time is 2021-02-28 18:58:33.237779

注意:它通过在 Python 编程中导入 datetime 模块,以字符串格式返回系统的当前日期。

now()方法的属性

Python now()方法具有类似于年、月、日、时、分、秒等时间属性的各种属性。

让我们创建一个程序来演示 now()方法的不同属性,以便在 Python 中打印日期。

程序 2.py


# importing the datetime module for now() method
import datetime
# Use datetime.now() method to get the current date into the variable current_date
current_date = datetime.datetime.now()
# print the message for now() attributes in Python
print (" Following are the attributes of now() function are : ")

# print the current year of the system
print(" The current year is : ", end = "" )
print (current_date.year)
# print the current month of the system
print( " The current month is : ", end = "" )
print (current_date.month)
# print the current date of the system
print( " The current date is : ", end = "" )
print (current_date.day)

输出:

Following are the attributes of now() function are :
 The current year is: 2021
 The current month is: 2
 The current date is: 28

使用 date.today()方法

出现在日期时间模块中的日期模块类,用于返回包含今天日期值的日期对象。其中在 Python 程序中使用 today()方法来获取今天的日期。

语法:


date.today()

让我们考虑一个使用 date 类的 today()方法显示系统当前日期的程序。

程序


from datetime import date
td = date.today() # Use the today method and assign it to the td variable.
print(" Get the today date in Python is: ", td)

输出:

Get the today date in Python is:  2021-02-28

使用 now()。日期()函数

a 现在()。date()方法,用于通过导入 Python 中的 datetime 模块来访问系统的本地或当前日期。

语法:


Current_date = datetime.now().date()

让我们编写一个程序,使用 datetime.now()打印系统的当前日期。Python 中的 date() 方法。

程序 3.py


from datetime import datetime
#  Return the current date of the system 
Current_date = datetime. now(). date() 
print("The current date is :", Current_date)

输出:

The current date is : 2021-02-28

使用 now()。时间()函数

a 现在()。time()方法,用于通过从 Python 中的 datetime 模块导入 time()方法来访问系统的本地或当前时间。

语法:


Current_date = datetime.now().time()

让我们编写一个程序,使用 datetime.now()打印系统的当前时间。Python 中的 time()方法。

Prog2.py


from datetime import datetime
# Return the current date and time of the system 
Current_time = datetime.now().time() 
print("Current time is :", Current_time)

输出:

Current time is: 19:07:45.787512

Python 中的日期和时间格式

下面是日期和时间的各种格式,通过将参数传递给str time()函数,可以在 Python 中获得所需的日期时间表示。

| 管理的 | 描述 | 例子 |
| %a | 显示缩写的工作日名称。 | 珊,我的… |
| %A | 显示完整的工作日名称。 | 周日,周一,.. |
| %b | 显示缩写的月份名称。 | 一月,二月,…,十二月 |
| %B | 显示完整月份的名称。 | 一月,二月,…十二月 |
| %c | 它用于定位适当的日期和时间表示。 | 孙 2021 年 2 月 21 日 07:05: 28 |
| %d | 以零填充的十进制格式显示一个月中的某一天。 | 01, 02, …, 28, 30, 31 |
| %H | 以零填充十进制数字的格式显示小时(24 小时制)。 | 00, 01, …, 23 |
| %I | 它将小时(12 小时制)表示为十进制数。 | 01, 02, …, 12 |
| %j | 以零填充十进制数字的形式显示一年中的某一天。 | 001, 002, …, 999 |
| %m | 以零填充十进制数的形式显示月份。 | 01, 02, …, 12 |
| %M | 以零填充十进制数的形式显示分钟。 | 00, 01, .., 59 |
| %p | 它用于定位上午或下午的等价物。 | 上午,下午 |
| %S | 以零填充十进制数的形式显示第二个。 | 00, 01, …, 59 |
| %U | 将星期日算作一年中星期几的第一天,它以十进制数字显示这一天。 | 00, 01, …, 53 |
| %w | 它用于将工作日定位为十进制数,其中周日以 0 开头。 | 00, 01, …, 53 |
| %W | 将星期一算作一年中星期几的第一天,它以十进制数字显示这一天。 | 00, 01, …, 53 |
| %x | 它用于定位日期的适当表示。 | 19- 02 – 2021 |
| %X | 它用于定位时间的适当表示。 | 07 : 41: 29 |
| %y | 以十进制数字的形式显示不包含世纪的年份格式。 | 0, 1, .., 99 |
| %Y | 以十进制数字的形式显示包含世纪的年份。 | 2014 年、2019 年等。 |
| %z | 它以+HHMM 或- HHMM 的形式表示世界协调时偏移。 | |
| %Z | 它基本上用于表示时区名称(如果不存在时区,则不包含任何字符)。 | |
| %% | 它只是表示一个字面值“%”字符。 | % |

让我们创建一个 Python 程序,并使用上述格式。

Prog1.py


import datetime
dt = datetime.datetime.now()
print ("Current date and time is = %s" % dt)
print ("Date and time in ISO Format = %s" % dt.isoformat())
print ("Current year = %s" %dt.year)
print ("Current month is = %s" %dt.month)
print ("Current date (day) = %s" %dt.day )
print ("represent Date in dd/mm/yyyy format = %s / %s/ %s" % (dt.day, dt.month, dt.year))
print ("Current hour is = %s" %dt.hour)
print (" Current minute is = %s" %dt.minute)
print ("Current Second is = %s" %dt.second)
print ("Representation of time in hh: mm: ss format = %s: %s: %s" % (dt.hour, dt.minute, dt.second))

输出:

Current date and time is = 2021-02-28 19:09:03.739466
Date and time in ISO Format = 2021-02-28T19:09:03.739466
Current year = 2021
Current month is = 2
Current date (day) = 28
Represent Date in dd/mm/yyyy format = 28 / 2/ 2021
Current hour is = 19
 Current minute is = 9
Current Second is = 3
Representation of time in hh: mm: ss format = 19: 9: 3

使用 strftime()函数

让我们使用 strftime()函数创建一个显示当前日期和时间的程序。

Strftime.py


import datetime
dt  = datetime.datetime.now()
print (dt.strftime ("%Y - %m - %d %H: %M: %S " )) # represents the date YYYY- MM - DD
# HH: MM: SS
print (dt.strftime ("%d / %m / %Y ")) # represents the date in DD/ MM/ YYYY
print (dt.strftime (" %I: %M: %S %p ")) # represents the time in HH: MM: SS AM/ PM
print (dt.strftime ("%a, %b %d, %Y ")) # represents the day, month date and year

输出:

2021 - 02 - 28 19: 12: 06
28 / 02 / 2021
 07: 12: 06 PM
Sun, Feb 28, 2021

原创文章,作者:简单一点,如若转载,请注明出处:https://www.506064.com/n/130560.html

(1)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
简单一点的头像简单一点
上一篇 2024-10-03 23:29
下一篇 2024-10-03 23:29

相关推荐

  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29

发表回复

登录后才能评论