Displaying date and time

You can use QDate, QTime, QDateTime classes from QtCore module to display date and time on the application.

For more details, see link below.


Displaying date

The QDate class provides date-related features.


Set the date format

With the format parameter of toString() method, you can set the format of the date.

from PyQt5.QtCore import QDate, Qt

now = QDate.currentDate()
print(now.toString('d.M.yy'))
print(now.toString('dd.MM.yyyy'))
print(now.toString('ddd.MMMM.yyyy'))
print(now.toString(Qt.ISODate))
print(now.toString(Qt.DefaultLocaleLongDate))

‘d’ stands for day, ‘M’ for month, ‘y’ for year. The format of the date varies depending on the number of characters.

By typing Qt.ISODate, Qt.DefaultLocaleLongDate, you can print to the default settings for the ISO standard format or application. The results are as follows.

2.1.19
02.01.2019
.1.2019
2019-01-02
2019 1 2 수요일

See the table below or refer to the official document for further details.

../_images/2_9_date_format.PNG ../_images/2_9_date_format_2.PNG

Displaying time

You can use QTime class to print current time.


Set the time format

from PyQt5.QtCore import QTime, Qt

time = QTime.currentTime()
print(time.toString('h.m.s'))
print(time.toString('hh.mm.ss'))
print(time.toString('hh.mm.ss.zzz'))
print(time.toString(Qt.DefaultLocaleLongDate))
print(time.toString(Qt.DefaultLocaleShortDate))

‘h’ stands for hour, ‘m’ for minute, ‘s’ for seconds, and ‘z’ stands for 1/1000 seconds.

Just like what we did with date formatting, you can use Qt.DefaultLocaleLongDate or Qt.DefaultLocaleShortDate to set the format for time.

The results are as follows.

16.2.3
16.02.03
16.02.03.610
오후 4:02:03
오후 4:02

See the table below or refer to the official document for further details.

../_images/2_9_time_format.PNG

Displaying date and time

You can use QDateTime class to print current date and time together.


Set date and time format

from PyQt5.QtCore import QDateTime, Qt

datetime = QDateTime.currentDateTime()
print(datetime.toString('d.M.yy hh:mm:ss'))
print(datetime.toString('dd.MM.yyyy, hh:mm:ss'))
print(datetime.toString(Qt.DefaultLocaleLongDate))
print(datetime.toString(Qt.DefaultLocaleShortDate))

Like the examples above, you can use ‘d’, ‘M’, ‘y’ for date and ‘h’, ‘m’, ‘s’ for time to set the format in which date and time is displayed.

Also, you can input Qt.DefaultLocaleLongDate or Qt.DefaultLocaleShortDate.


Displaying date on statusbar

Let’s print today’s date on the status bar.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QDate, Qt


class MyApp(QMainWindow):

    def __init__(self):
        super().__init__()

        self.date = QDate.currentDate()
        self.initUI()

    def initUI(self):

        self.statusBar().showMessage(self.date.toString(Qt.DefaultLocaleLongDate))

        self.setWindowTitle('Date')
        self.setGeometry(300, 300, 400, 200)
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

Use currentDate() method to get today’s date and use showMessage() method to display the date on the status bar.


../_images/2_9_showing_date.png

Figure 3-9. Displaying date on statusbar.

Prev/Next