Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

python - How to add string and datetime format

i have a string like this (YYYY/MM/DD/HH/MM):

0000/01/00/00/00/00

I need to add this string and now's data. I give now's data via

datetime.now()

I try to use:

conv_data = datetime.strptime('0000/01/00/00/00/00', '%Y/%m/%d/%H/%M/%S')
conv_data + datetime.now()

but it doesn't work because Year, Month and etc. must be greater then zero. Can you help me to solve my problem, please.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Another way of doing so would be using split()

Ex.

from datetime import datetime
date_string = '0000/01/00/00/00/00'
date_array = date_string.split('/')

conv_data = datetime.strptime('-'.join(date_array [0:3])+" " + ':'.join(date_array [3:]), '%Y-%m-%d %H:%M:%S')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...