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://my.lmcjl.com/post/7462.html

展开阅读全文

4 评论

留下您的评论.