如何在 Python 中获取格式化的日期和时间?

pythonserver side programmingprogramming更新于 2025/2/26 22:04:17

在本文中,我们将向您展示如何在 Python 中格式化日期和时间。

Python 中的 datetime 模块提供了处理日期和时间值的方法。要使用此模块,我们必须首先使用以下 strftime() 函数导入它,该函数返回格式化的日期和时间。它接受格式字符串,您可以使用该字符串来获取所需的结果。以下是它支持的指令。

指令 含义
%a 语言环境的缩写星期名称
%B 语言环境的完整月份名称。
%c 语言环境的适当日期和时间表示
%d 以十进制数表示的月份天数 [01,31]。
%H 以十进制数表示的小时(24 小时制) [00,23]。
%I 小时(12 小时制)以十进制数表示 [01,12]。
%m 月份以十进制数表示 [01,12]。
%M 分钟以十进制数表示 [00,59]。
%p 区域设置的 AM 或 PM 等值。
%S 秒以十进制数表示[00,61]。
%U 一年中的周数(星期日为一周的第一天),以十进制数 [00,53] 表示。新年中第一个星期日之前的所有日子都被视为第 0 周。
%w 工作日,以十进制数 [0(星期日),6] 表示。
%W 一年中的周数(星期一为一周的第一天),以十进制数 [00,53] 表示。新年中第一个星期一之前的所有日子都被视为第 0 周。
%x 语言环境的适当日期表示。
%X 语言环境的适当时间表示。
%y 以十进制数表示的不带世纪的年份 [00,99]。
%Y 以十进制数表示的带世纪的年份。
%Z 时区名称(如果不存在时区,则无字符)。
%% A文字"%"字符

datetime 类的 Strftime() 函数

使用 date、time 或 datetime 对象,strftime() 方法返回表示日期和时间的字符串。

要使用指定格式将 datetime 对象转换为字符串,请使用 datetime.strftime(format)。

格式代码是用于指定要表示日期时间的格式的标准指令。例如,%d-%m-%Y%H:%M:%S 代码将日期转换为 dd-mm-yyyy hh:mm:ss 格式。

示例

from datetime import datetime # getting the current date and time current_datetime = datetime.now() # getting the year from the current date and time current_year = current_datetime.strftime("%Y") print("current year = ", current_year) # getting the month from the current date and time current_month = current_datetime.strftime("%m") print("current month = ", current_month) # getting the day/date from the current date and time current_day = current_datetime.strftime("%d") print("current day = ", current_day) # getting the time from the current date and time in the given format current_time = current_datetime.strftime("%H:%M:%S") print("current time = ", current_time) # getting the date and time from the current date and time in the given format current_date_time = current_datetime.strftime("%m/%d/%Y, %H:%M:%S") print("current date and time = ",current_date_time)

输出

current year = 2022
current month = 08
current day = 15
current time = 17:49:46
current date and time = 08/15/2022, 17:49:46

注意:如果程序在在线编辑器上运行,它会显示 GMT 时间,而如果程序在本地系统编辑器上运行,它会显示您自己显示的时间

时间类

时间 类可用于表示时间值。小时、分钟、秒和微秒是时间类属性。

语法

time(hour, minute, second, microsecond)

Example 1

import datetime # formating time format_time = datetime.time(3, 12, 24, 10) print(format_time)

输出

03:12:24.000010

时间属性有范围,例如,秒的范围是 0 到 59,纳秒的范围是 0 到 999999。如果范围太大,编译器会显示 ValueError

时间类的实例有三个实例属性:小时、分钟、秒和微秒。这些用于获取特定的时间信息

示例 2

import datetime # formating time format_time = datetime.time(3, 12, 24, 10) # Printing time in the formating of time attributes print('Time:', format_time.hour,' hours ', format_time.minute,' minutes ', format_time.second,'seconds and ', format_time.microsecond,'microseconds' )

输出

Time: 3 hours 12 minutes 24 seconds and 10 microseconds

日期类

可以使用日期类表示日历日期值。日期实例由年、月和日属性组成。

语法

date(yyyy, mm, dd)

示例 1

import datetime # formating date format_date = datetime.date(2005, 4, 16) # Printing date in the formating of date attributes print('Date:', format_date.day, 'day,', format_date.month, 'month and', format_date.year, 'year')

输出

Date: 16 day, 4 month and 2005 year

将日期转换为字符串

由于日期和时间与字符串不同,因此经常需要将日期时间转换为字符串。我们利用 strftime() 函数来实现此目的。

语法

time.strftime(format, t)

参数

format- 字符串类型。指令可以包含在格式字符串中

time - 应格式化的时间

示例

import datetime # formating date and time format_datetime = datetime.datetime(2005, 4, 16, 3, 22, 30, 8) # Printing date & time in the formating of date and time attributes print(format_datetime.strftime("%b %d %Y %H:%M:%S"))

输出

Apr 16 2005 03:22:30

符号 %H、%M 和 %S 分别代表小时、分钟和秒。字符 %b、%d 和 %Y 分别代表月、日和年。

将字符串转换为日期

在处理从 csv 导入的数据集或从网站表单获取输入时,经常需要将字符串转换为日期。Python 有一个名为 strptime() 的函数可以实现此操作。

语法

datetime.strptime(string, format)

参数

string- 应格式化的字符串

format - 字符串类型。指令可以包含在格式字符串中。

示例

from datetime import datetime # converting input string to date format(date-month-year format) print(datetime.strptime('16/5/2005', '%d/%m/%Y'))

输出

2005-05-16 00:00:00

结论

我们学习了如何使用 datetime 模块在 Python 中格式化日期和时间。我们还学习了如何通过将日期转换为字符串然后再转换为日期来格式化日期。


相关文章


有用资源