📌  相关文章
📜  在Python中将日期时间字符串转换为 YYYY-MM-DD-HH:MM:SS 格式

📅  最后修改于: 2022-05-13 01:54:43.139000             🧑  作者: Mango

在Python中将日期时间字符串转换为 YYYY-MM-DD-HH:MM:SS 格式

在本文中,我们将把 DateTime字符串转换为 %Y-%m-%d-%H:%M:%S 格式。对于此任务,使用 strptime() 和 strftime()函数。

strptime()用于将DateTime字符串转换为年-月-日时分秒格式的DateTime

句法:

strftime()用于将 DateTime 转换为所需的时间戳格式



语法

这里,

  • &Y 表示年份
  • %m 表示月份
  • %d 表示天
  • %H 表示小时
  • %M 表示分钟
  • %S 表示秒。

首先将 DateTime 时间戳作为字符串。然后,使用 strptime() 将其转换为 DateTime。现在,使用 strftime 转换为必要的 DateTime 格式

示例1: Python程序将DateTime字符串转换为%Y-%m-%d-%H:%M:%S格式

Python3
# import datetime module
from datetime import datetime
  
# consider date in string format
my_date = "30-May-2020-15:59:02"
  
# convert datetime string into date,month,day and
# hours:minutes:and seconds format using strptime
d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S")
  
# convert datetime format into %Y-%m-%d-%H:%M:%S
# format using strftime
print(d.strftime("%Y-%m-%d-%H:%M:%S"))


Python3
# import datetime module
from datetime import datetime
  
# consider date in string format
my_date = "30-May-2020-15:59:02"
  
# convert datetime string into date,month,day 
# and hours:minutes:and seconds format using
# strptime
d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S")
  
# convert datetime format into %Y-%m-%d-%H:%M:%S
# format using strftime
print(d.strftime("%Y-%m-%d-%H:%M:%S"))
  
# consider date in string format
my_date = "04-Jan-2021-02:45:12"
  
# convert datetime string into date,month,day 
# and hours:minutes:and seconds format using 
# strptime
d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S")
  
# convert datetime format into %Y-%m-%d-%H:%M:%S 
# format using strftime
print(d.strftime("%Y-%m-%d-%H:%M:%S"))
  
# consider date in string format
my_date = "23-May-2020-15:59:02"
  
# convert datetime string into date,month,day and 
# hours:minutes:and seconds format using strptime
d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S")
  
# convert datetime format into %Y-%m-%d-%H:%M:%S 
# format using strftime
print(d.strftime("%Y-%m-%d-%H:%M:%S"))


输出

'2020-05-30-15:59:02'

示例2: Python程序将DateTime字符串转换为%Y-%m-%d-%H:%M:%S格式

蟒蛇3

# import datetime module
from datetime import datetime
  
# consider date in string format
my_date = "30-May-2020-15:59:02"
  
# convert datetime string into date,month,day 
# and hours:minutes:and seconds format using
# strptime
d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S")
  
# convert datetime format into %Y-%m-%d-%H:%M:%S
# format using strftime
print(d.strftime("%Y-%m-%d-%H:%M:%S"))
  
# consider date in string format
my_date = "04-Jan-2021-02:45:12"
  
# convert datetime string into date,month,day 
# and hours:minutes:and seconds format using 
# strptime
d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S")
  
# convert datetime format into %Y-%m-%d-%H:%M:%S 
# format using strftime
print(d.strftime("%Y-%m-%d-%H:%M:%S"))
  
# consider date in string format
my_date = "23-May-2020-15:59:02"
  
# convert datetime string into date,month,day and 
# hours:minutes:and seconds format using strptime
d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S")
  
# convert datetime format into %Y-%m-%d-%H:%M:%S 
# format using strftime
print(d.strftime("%Y-%m-%d-%H:%M:%S"))
输出
2020-05-30-15:59:02
2021-01-04-02:45:12
2020-05-23-15:59:02