from datetime import datetime as dt tstr = '2012-12-29 13:49:37' tdatetime = dt.strptime(tstr, '%Y-%m-%d %H:%M:%S') strptimeの第二引数は第一引数のフォーマットを渡す。 例えば、 tstr = '2012/12/29 13:49:37'だった場合、 dt.strptime(tstr, '%Y/%m/%d %H:%M:%S') 文字列から日付(date) import datetime tstr = '2012-12-29 13:49:37' tdatetime = datetime.datetime.strptime(tstr, '%Y-%m-%d %H:%M:%S') tdate = datetime.date(tdatetime.year, tdatetime.mo
![Pythonで文字列 <-> 日付(date, datetime) の変換 - Qiita](https://cdn-ak-scissors.b.st-hatena.com/image/square/457feb8f00a80015f286f52b4be15079f2433d4b/height=288;version=1;width=512/https%3A%2F%2Fqiita-user-contents.imgix.net%2Fhttps%253A%252F%252Fcdn.qiita.com%252Fassets%252Fpublic%252Farticle-ogp-background-412672c5f0600ab9a64263b751f1bc81.png%3Fixlib%3Drb-4.0.0%26w%3D1200%26mark64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZ3PTk3MiZoPTM3OCZ0eHQ9UHl0aG9uJUUzJTgxJUE3JUU2JTk2JTg3JUU1JUFEJTk3JUU1JTg4JTk3JTIwJTNDLSUzRSUyMCVFNiU5NyVBNSVFNCVCQiU5OCUyOGRhdGUlMkMlMjBkYXRldGltZSUyOSUyMCVFMyU4MSVBRSVFNSVBNCU4OSVFNiU4RiU5QiZ0eHQtYWxpZ249bGVmdCUyQ3RvcCZ0eHQtY29sb3I9JTIzMUUyMTIxJnR4dC1mb250PUhpcmFnaW5vJTIwU2FucyUyMFc2JnR4dC1zaXplPTU2JnM9NTQ4YjAyMDQyYTE4OTRiM2EwY2ZkODQ2NGZlMWQxMjM%26mark-x%3D142%26mark-y%3D57%26blend64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZoPTc2Jnc9NzcwJnR4dD0lNDBzaGliYWludXJvdSZ0eHQtY29sb3I9JTIzMUUyMTIxJnR4dC1mb250PUhpcmFnaW5vJTIwU2FucyUyMFc2JnR4dC1zaXplPTM2JnR4dC1hbGlnbj1sZWZ0JTJDdG9wJnM9OWNjMDNmNzMwYzhhNzZmN2RjNTQ5NDViMWM3OWI3MTE%26blend-x%3D142%26blend-y%3D486%26blend-mode%3Dnormal%26s%3Dea4bf7ade12801f57dc64ac478fb91db)