Qdate,获得一个月的第一天

时间:2016-04-03 19:22:33

标签: python qt python-2.7 pyside

我正在为PySide(QcalendarWidget)中的日历编写每周时间表。例如,在第一周,我希望在本周的每一天都有任务A,B,C,D。每个月第一周(前7天)的字典是这样的:

FirstWeekTasks={1:["A","B"],3:["C","D"],5:["A","D"]}

其中1,3和5是当月的第一天,第三天和第五天。

我该怎么做? 感谢

2 个答案:

答案 0 :(得分:0)

使用time模块:

>>> import time
>>> time.localtime().tm_mday
4

使用datetime模块:

>>> import datetime
>>> datetime.datetime.now().day
4

答案 1 :(得分:0)

You can do this with QDate.addDays().

Start with the selected date from the calendar widget:

>>> seldate = calwidget.selectedDate()
>>> seldate
PySide.QtCore.QDate(2016, 4, 19)

Then to get the required days of the selected calendar month, just subtract the day():

>>> seldate.day()    
19
>>> seldate.addDays(1 - seldate.day()) # first
PySide.QtCore.QDate(2016, 4, 1)
>>> seldate.addDays(3 - seldate.day()) # third
PySide.QtCore.QDate(2016, 4, 3)
>>> seldate.addDays(5 - seldate.day()) # fifth
PySide.QtCore.QDate(2016, 4, 5)

So addDays() can take a positive number to go forwards in time, and a negative number to go backwards.