如何将datetime从tkcalendar转换为字符串?

2024-04-28 23:00:37 发布

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

我正在创建一个从用户处获取日期的程序,现在我需要转换为字符串,因为我需要将这些数据放入我正在实现的命令shell中,使用tkinter和tkcalendar(该lib为我提供获取条目的功能)。 我该怎么做

下面是我要构建的在git上运行的代码:

cal = Calendar(top, font='Arial 14', selectmode='day', cursor='hand1')
subprocess.call('git log --pretty='"format: % h"' --after=' +cal+' --before='+cal, shell=True)

Tags: 数据字符串代码用户git命令程序目的
1条回答
网友
1楼 · 发布于 2024-04-28 23:00:37

在您的例子中,cal是一个Calendar实例,cal.datetime返回python的datetime对象,该对象有strftime将日期时间格式化为字符串:

dt = cal.datetime.strftime("%Y-%m-%d")
subprocess.call(f"""git log  pretty="format: %h"  after={dt}  before={dt}""", shell=True)

相关问题 更多 >