Python 相当于日期旋转框的 Tcl "clock add" 命令?
Python equivalent of Tcl "clock add" command for date-spinning spinbox?
我用 Tcl 编写了一个程序,可以让旋转框以 YYYY/MM/DD 格式旋转日期,如下所示:
proc datespin {way w} {
set datoa [$w get] ; # current date from spinbox
set oldsecs [clock scan $datoa -format {%Y/%m/%d}]
if {$way eq "up"} {
set newsecs [clock add $oldsecs 1 day]
} else {
set newsecs [clock add $oldsecs "-1" day]
}
set datoa [clock format $newsecs -format {%Y/%m/%d}]
$w delete 0 end
$w insert 0 $datoa ; # new date
}
现在我正在尝试用 tkinter 重写 Python 中的程序,但我还没有找到一个简单的 Python 等效于 Tcl clock add
命令。有吗?有点儿?我查看了 time
和 datetime
模块文档,但我对 Python 还是个新手,有点迷茫。请帮忙!
您可能正在寻找 datetime.timedelta
。
>>> from datetime import date, timedelta
>>>
>>> current_date = date(year=2000, month=1, day=1)
>>> current_date.isoformat()
'2000-01-01'
>>>
>>> next_date = current_date + timedelta(days=1)
>>> next_date.isoformat()
'2000-01-02'
>>>
>>> previous_date = current_date - timedelta(days=1)
>>> previous_date.isoformat()
'1999-12-31'
您可以使用datetime.date.today
, and format dates using datetime.date.strftime
获取系统日期。例如:
>>> from datetime import date
>>> date.today().strftime('%Y/%m/%d')
我用 Tcl 编写了一个程序,可以让旋转框以 YYYY/MM/DD 格式旋转日期,如下所示:
proc datespin {way w} {
set datoa [$w get] ; # current date from spinbox
set oldsecs [clock scan $datoa -format {%Y/%m/%d}]
if {$way eq "up"} {
set newsecs [clock add $oldsecs 1 day]
} else {
set newsecs [clock add $oldsecs "-1" day]
}
set datoa [clock format $newsecs -format {%Y/%m/%d}]
$w delete 0 end
$w insert 0 $datoa ; # new date
}
现在我正在尝试用 tkinter 重写 Python 中的程序,但我还没有找到一个简单的 Python 等效于 Tcl clock add
命令。有吗?有点儿?我查看了 time
和 datetime
模块文档,但我对 Python 还是个新手,有点迷茫。请帮忙!
您可能正在寻找 datetime.timedelta
。
>>> from datetime import date, timedelta
>>>
>>> current_date = date(year=2000, month=1, day=1)
>>> current_date.isoformat()
'2000-01-01'
>>>
>>> next_date = current_date + timedelta(days=1)
>>> next_date.isoformat()
'2000-01-02'
>>>
>>> previous_date = current_date - timedelta(days=1)
>>> previous_date.isoformat()
'1999-12-31'
您可以使用datetime.date.today
, and format dates using datetime.date.strftime
获取系统日期。例如:
>>> from datetime import date
>>> date.today().strftime('%Y/%m/%d')