PyQt QDateTimeEdit

Summary: in this tutorial, you’ll learn how to create a date & time entry widget using the PyQt QDateTimeEdit class.

Introduction to the PyQt QDateTimeEdit widget

The QDateTimeEdit class allows you to create a widget for editing dates & times:

PyQt QDateTimeEdit

The QDateTimeEdit widget allows you to edit the date and time using the keyboard or up/down arrow keys to increase/decrease the value.

Also, you can use the left/right arrow key to move between the day, month, year, hour, and minute sections of the entry.

The QDateTimeEdit has the following useful methods and properties:

PropertyDescription
date()Return the date value displayed by the widget. The return type is QDate. To convert it to a datetime.date object, you use the toPyDate() method of the QDate class.
time()Return the time displayed by the widget. The return value has the type of QTime. Use the toPyTime() method to convert it to a Python datetime.time object.
dateTime()Return the date and time value displayed by the widget. The return type is QDateTime.
minimumDateThe earliest date that can be set by the user.
maximumDateThe latest date that can be set by the user.
minimumTimeThe earliest time that can be set by the user.
maximumTimeThe latest time that can be set by the user.
minimumDateTimeThe earliest date & time that can be set by the user.
maximumDateTimeThe latest date & time that can be set by the user.
calendarPopupDisplay a calendar popup if it is True.
displayFormatis a string that formats the date displayed in the widget.

The QDateTimeEdit emits the editingFinished() signal when the editing is finished. If you want to trigger an action whenever the value of the QDateTimeEdit widget changes, you can connect to the dateTimeChanged() signal.

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

PyQt QDateTimeEdit widget example

The following program uses the QDateTimeEdit class to create a widget for editing date & time:

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('PyQt 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("yyyy-MM-dd HH:mm"))


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

How it works.

First, create the QDateTimeEdit widget:

self.datetime_edit = QDateTimeEdit(self, calendarPopup=True)Code language: Python (python)

Second, connect the dateTimeChanged() signal to the update() method:

self.date_edit.dateTimeChanged.connect(self.update)Code language: Python (python)

Third, create a QLabel widget to display the value of the date_edit widget once the editing is finished:

self.result_label = QLabel('', self)Code language: Python (python)

Finally, define the update() method that updates the label widget with the current value of the date & time entry:

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

Output:

Summary

  • Use the QDateTimeEdit to create a date and time entry widget.
  • Use the dateTime() method to get the current value of the QDateTimeEdit widget
Did you find this tutorial helpful ?