PyQt - QDateTimeEdit



A datetime is a set of integer values that contain both date and time parts. Datetime plays a crucial role in our lives because it helps us organize the plans, meetings and coordinate activities efficiently.

A class QDateTimeEdit come up with a widget for editing date and time.

This method allows users to change the dates by using keyboard or arrow keys. User can either increase or decrease the date and time values. The QDateTimeEdit box allows users to navigate between sections using arrow keys, and the dates and times are displayed based on the format set using setDisplayFormat() method.

Following is the list of methods and their properties in QDateTimeEdit class−

  • time() − The widgets return the time as display output. It also returns the type Qtime. Using the method toPytime(), it converts datetime.time object.
  • datetime() − The method returns QDateTime as type. Here, widgets display the result by returning the date and time value.
  • minimumDate − User can set the earliest date.
  • maximumDate − User can set the new or current date.
  • minimumTime − User can set the earliest time.
  • maximumTime − User can set the new or current time.
  • minimumDateTime − Using this method users can easily set the earliest date and time together.
  • maximumDateTime − Using this method users can set the new or current date and time.
  • calendarPopup − A calendarPopUp is defined as a parameter inside the QDateTime() method. If the value is assigned to true then it displays a calendar.
  • displayFormat − This method defines the string formats and it displays in the widget.

The QDateTimeEdit widget emits the editingFinished() signal when editing is complete. We can connect to the dateTimeChanged() signal if you want to trigger an action every time the widget's value changes.

If we need to create a widget for editing dates, we can use QDateEdit. Similarly, you can use QTimeEdit to create a widget for editing times only.

Example 1

In this example, we create a functional widget box that allows the user to edit the date and time.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QDateTimeEdit, QVBoxLayout
class DateTimeEditDemo(QWidget):
   def __init__(self):
      super().__init__()
      self.init_ui()
   def init_ui(self):
      self.setWindowTitle("QDateTimeEdit")
      self.setGeometry(100, 100, 400, 200)

      # Create a QDateTimeEdit widget
      datetime_edit = QDateTimeEdit(self)
      # Set the display format
      datetime_edit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")

      # Set date and time range (optional)
      datetime_edit.setMinimumDateTime(datetime_edit.minimumDateTime().addDays(-365))
      datetime_edit.setMaximumDateTime(datetime_edit.maximumDateTime().addDays(365))

      # Add the widget to the layout
      layout = QVBoxLayout()
      layout.addWidget(datetime_edit)
      self.setLayout(layout)

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = DateTimeEditDemo()
   window.show()
   sys.exit(app.exec())

Output

The above code produces the following output-

QStatusBar

Example 2

We create a widget container which allows user to select the date through calendar and for time it allows to edit the time with AM/PM format.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, 
QDateTimeEdit, QLabel, QFormLayout
class MainWindow(QWidget):
   def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)

      self.setWindowTitle('QDateTimeEdit')
      self.setMinimumWidth(200)

      layout = QFormLayout()
      self.setLayout(layout)

      self.datetime_edit = QDateTimeEdit(self, calendarPopup=True)
      self.datetime_edit.dateTimeChanged.connect(self.update)

      self.result_label = QLabel('', self)

      layout.addRow('Date:', self.datetime_edit)
      layout.addRow(self.result_label)

      self.show()

   def update(self):
      value = self.datetime_edit.dateTime()
      self.result_label.setText(value.toString("dd-MM-yyyy HH:mm"))

if __name__ == '__main__':
   app = QApplication(sys.argv)
   window = MainWindow()
   sys.exit(app.exec())

Output

The above code produces the following output-

QStatusBar
Advertisements