如何将Git日志日期格式转换为整数?

2024-10-03 15:22:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从git日志中存储一个日期来比较它们,但是当我把它们存储到一个数组中时,它说它们是字符串类型的,我不知道如何转换这种格式(e.qcommited date: Mon Aug 22 15:43:38 2016 +0200)。在

date = commits[i]['Date']  
print("commited date:", date) 
moduleDate.append(date)
upToDateModule = max(moduleDate) #trigger here

Traceback (most recent call last):   File
"/home/savoiui/PycharmProjects/VersionChecker/versionCheckerV4.py",
line 120, in <module>
    main()   File "/home/savoiui/PycharmProjects/VersionChecker/versionCheckerV4.py",
line 110, in main
    upToDateModule = max(moduleDate)
 TypeError: an integer is required (got type str)

Tags: inpygithomedatemainlinemax
2条回答

要从字符串中获取日期时间,可以使用datetime库和strptime()方法。在

NB: Did you use // to comment? In python, you comment with the # character

在将日期字符串附加到moduleDate列表之前,可以尝试将日期字符串转换为datetime格式,如下所示

from datetime import datetime

date = commits[i]['Date']
print("commited date:", date) 
# commited date: Mon Aug 22 15:43:38 2016 +0200

datetime_object = datetime.strptime(' '.join(date.split(' ')[:-1]), '%a %b %d %H:%M:%S %Y') 

moduleDate.append(datetime_object) 
upToDateModule = max(moduleDate)

希望这有帮助!在

相关问题 更多 >