Reverse str to datetime.datetime in Python
如何将字符串"2014-03-16 18:28:11"返回到datetime.datetime。我想知道是否有比下面更简单的方法
1 2 3 4 5 6 7 8 9 | >>> datetime.datetime(2014, 3, 16, 18, 28, 11) >>> str(datetime.datetime(2014, 3, 16, 18, 28, 11)) '2014-03-16 18:28:11' >>> complete = str(datetime.datetime(2014, 3, 16, 18, 28, 11)) >>> date, time = complete.split("") >>> [int(x) for x in date.split("-")] [2014, 3, 16] >>> [int(x) for x in time.split(":")] [18, 28, 11] |
然后使用每个变量作为datetime.datetime的输入
你要的是
1 | date = datetime.datetime.strptime(complete, '%Y-%m-%d %H:%M:%S') |