[英]How to format Date in Python that is retrieve from a table?
This is my code where I'm writing my data into a file.这是我将数据写入文件的代码。 How do I convert my dob variable into a different date format.如何将我的 dob 变量转换为不同的日期格式。 Dob Currently shoes up in the file as: 2020-04-30 00:00:00 but I want it to show as 4/30/2020. Dob 目前在文件中显示为:2020-04-30 00:00:00 但我希望它显示为 4/30/2020。 Any suggestions?有什么建议么?
with open(r"C:\inetpub\ftproot\mhv\mhv_exportdetail.txt",'w') as f:
for companyrow in company_rows:
# Joining the ints,and strings in the tuple to convert to a string
newRow = '\t'.join(map(str,companyrow))
#print(newRow)
dob =(companyrow[12])
from datetime import datetime
oldformat = '2020-04-30 00:00:00'
dob = datetime.strptime(oldformat,'%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y')
print (dob)
prints:印刷:
04/30/2020
Something like this should work for you.这样的事情应该适合你。
from datetime import datetime
previous_datetime = "2020-04-30 00:00:00"
new_datetime = datetime.strptime(previous_datetime, '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y')
print(new_datetime)
Which will output:这将是 output:
04/30/2020
I would recommend learning the strftime() and strptime() Format Codes , as they can prove very useful.我建议学习strftime() 和 strptime() 格式代码,因为它们非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.