简体   繁体   English

将日期转换为特定格式时出错

[英]Error when converting date to specific format

I have the following format for my dates 1952-02-04 00:00:00 我的约会日期格式如下:1952-02-04 00:00:00

I need the format to be month/day/year 我需要格式为month/day/year

How do I go about this currently I have 我目前该如何处理

if clientDob is not None:
            clientDob = datetime.datetime.strptime(clientDob, '%Y-%m-%d').strftime('%m/%d/%y')

the error I get is 我得到的错误是

time data 'Birth Dt' does not match format '%Y-%m-%d' 时间数据“生日Dt”与格式“%Y-%m-%d”不匹配

Use 采用

clientDob = datetime.datetime.strptime(clientDob, '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%y')

for datetime parsing your string must match the source string exactly . 对于日期时间,您的字符串必须与源字符串完全匹配。 It contains time-information, you need to match it. 它包含时间信息,您需要对其进行匹配。

import datetime 
clientDob = "2018-12-21 12:13:14"

clientDob = datetime.datetime.strptime(clientDob, '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%y')
print(clientDob)

Output: 输出:

12/21/18

Details: strftime-and-strptime-behavior 详细信息: strftime和strptime行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM