比较str日期(来自数据库)和QDateTimeEdit值

时间:2019-03-27 15:19:58

标签: python datetime qt5

我想比较从数据库中解析的字符串与QDateTimeEdit(由qt设计器创建)的DateTime值。 最好的解决方案是什么?

我的QDateTimeEdit名为qDateTimeFrom。 我的str对象名为strFrom。

dateFrom = self.qDateTimeFrom.dateTime()
strFrom = '2018-01-05-22.49.15.630000'
 # convert (str to QDateTime object?)
qstrFrom = QtCore.QDateTime.fromString(strFrom, "yyyy-MM-dd-HH:mm:ss")
 # compare
if(dateFrom > qstrFrom ):
  print('dateFrom is bigger than strFrom')
elif(dateFrom == qStrFrom):
  print('dateFrom is exactly strFrom')

1 个答案:

答案 0 :(得分:0)

这是我的解决方法:

dateFrom = self.qDateTimeFrom.dateTime()
strFrom = '2018-01-05-22.49.15.630000'

 # convert (QDateTimeEdit to string; then string to DateTime)
dateFrom = dateFrom.toString(self.dateFrom.displayFormat())
dateFromDT = datetime.strptime(str(dateFrom),'%d.%m.%Y %H:%M')

 # convert (String to DateTime)
strFromDT = datetime.strptime(str(strFrom),'%Y-%m-%d-%H.%M.%S.%f')

 # compare
if(dateFromDT > strFromDT ):
  print('dateFromDT is bigger than strFromDT')
print('Difference: ' + str(dateFromDT - strFromDT))