简体   繁体   中英

How to convert a date format (YYYY-MM-DD) to (YYYY,MM,DD) in Python

How to convert a date format (YYYY-MM-DD) to (YYYY,MM,DD) in Python

OR, I have a date = 2020-06-11 which I want to convert into seconds.

Works: d2 = (dt.datetime(2020,6,30) - t0).total_seconds()

Fails: d2 = (dt.datetime(2020-6-30) - t0).total_seconds()

How do I do it?

If you need to cast a string to a datetime object, you can use strptime for this.

from datetime import datetime

t = datetime.strptime('2020-06-11', '%Y-%m-%d')

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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