Python date object for MongoDB
我需要根据用户输入(字符串)准备插入MongoDB的日期。
代码:
1 2 3 4 5 6 | from datetime import datetime import time ... self.d_birthdate = time.strptime('6/8/1980', '%m/%d/%Y') self.d_created = dict.get('d_created', datetime.now()) ... |
可以将
1 2 3 4 5 | from datetime import datetime from time import mktime birthdate = time.strptime('6/8/1980', '%m/%d/%Y') self.d_birthdate = datetime.fromtimestamp(mktime(birthdate)) |
在更新版本的python中(我敢肯定在2.4中您不能做到这一点),您可以做到:
1 | self.d_birthdate = datetime.strptime('6/8/1980', '%m/%d/%Y') |
号