pyqt5显示日期与时间

PyQt5具有QDate,QDateTime,QTime类来处理日期和时间。 QDate是用于使用公历中的日历日期的类。 它具有确定日期,比较或处理日期的方法。 QTime类使用时钟时间。 它提供了比较时间,确定时间的方法以及其他各种时间操纵方法。 QDateTime是将QDate和QTime对象都组合到一个对象中的类。

1. 当前日期和时间

PyQt5具有currentDate(),currentTime()和currentDateTime()方法来确定当前日期和时间。

#fileName: current_date_time.py
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt

now = QDate.currentDate()
print(now.toString(Qt.ISODate))
print(now.toString(Qt.DefaultLocaleLongDate))

datetime = QDateTime.currentDateTime()

print(datetime.toString())

time = QTime.currentTime()
print(time.toString(Qt.DefaultLocaleLongDate))

2.UTC时间

#fileName: utc_local.py

from PyQt5.QtCore import QDateTime, Qt

now = QDateTime.currentDateTime()

print("Local dateTime:", now.toString(Qt.ISODate))
print("Universal datetime:", now.toUTC().toString(Qt.ISODate))

print("The offset from UTC is: {0} seconds".format(now.offsetFromUtc()))

3.计算天数差异

#fileName: xmas.py

from PyQt5.QtCore import QDate

xmas1 = QDate(2016, 12, 24)
xmas2 = QDate(2017, 12, 24)

now = QDate.currentDate()

dayspassed = xmas1.daysTo(now)
print("{0} days have passed since last XMas".format(dayspassed))

nofdays = now.daysTo(xmas2)
print("There are {0} days until next XMas".format(nofdays))

4.日期时间算法

#fileName:arithmetic.py

from PyQt5.QtCore import QDateTime, Qt

now = QDateTime.currentDateTime()

print("Today:", now.toString(Qt.ISODate))
print("Adding 12 days:{0}".format(now.addDays(12).toString(Qt.ISODate)))
print("Subtracting 22 days:{0}".format(now.addDays(-22).toString(Qt.ISODate)))

print("Adding 50 seconds:{0}".format(now.addSecs(50).toString(Qt.ISODate)))
print("Adding 3 seconds:{0}".format(now.addSecs(3).toString(Qt.ISODate)))
print("Adding 12 seconds:{0}".format(now.addSecs(12).toString(Qt.ISODate)))

5.夏令时

#fineName: daylight_saving.py

from PyQt5.QtCore import QDateTime, QTimeZone, Qt

now = QDateTime.currentDateTime()

print("Time zone:{0}".format(now.timeZoneAbbreviation()))

if now.isDaylightTime():
    print("The current date falls into DST time")
else:
    print("The current date does not fall into DST time")

6.Unix时间

#fileName: unix_time.py

from PyQt5.QtCore import QDateTime, Qt

now = QDateTime.currentDateTime()

unix_time = now.toSecsSinceEpoch()
print(unix_time)

d = QDateTime.fromSecsSinceEpoch(unix_time)
print(d.toString(Qt.ISODate))

7.朱利安日

#fileName: julian_day.py
from PyQt5.QtCore import QDate, Qt

now = QDate.currentDate()

print("Gregorian date for today:", now.toString(Qt.ISODate))
print("Julian day for today:", now.toJulianDay())

8.历史战役

#fileName: battles.py

from PyQt5.QtCore import QDate, Qt

borodino_battle = QDate(1812, 9, 7)
slavkov_battle = QDate(1805, 12, 2)

now = QDate.currentDate()

j_today = now.toJulianDay()
j_borodino = borodino_battle.toJulianDay()
j_slavkov = slavkov_battle.toJulianDay()

d1 = j_today - j_slavkov
d2 = j_today - j_borodino

print("Days since Slavkov battle:{0}".format(d1))
print("Days since Borodino battle:{0}".format(d2))

参考网址:

http://zetcode.com/gui/pyqt5/datetime/

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Python Pyqt5 代码示例,用于在界面上显示当前的日期时间,注释详细: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout from PyQt5.QtCore import QTimer, QDateTime class DateTimeApp(QWidget): def __init__(self): super().__init__() # 设置窗口大小和标题 self.setGeometry(100, 100, 400, 200) self.setWindowTitle("Date Time App") # 添加标签用于显示日期时间 self.date_label = QLabel(self) self.time_label = QLabel(self) # 创建垂直布局 layout = QVBoxLayout() # 将标签添加到布局中 layout.addWidget(self.date_label) layout.addWidget(self.time_label) # 设置布局 self.setLayout(layout) # 定时器用于实时更新时间 timer = QTimer(self) timer.timeout.connect(self.update_time) timer.start(1000) # 显示窗口 self.show() def update_time(self): # 获取当前日期时间并更新标签 current_time = QDateTime.currentDateTime() current_date = current_time.toString("yyyy-MM-dd") current_time = current_time.toString("hh:mm:ss") self.date_label.setText(current_date) self.time_label.setText(current_time) if __name__ == "__main__": app = QApplication(sys.argv) date_time_app = DateTimeApp() app.exec_() ``` 这段代码使用了 PyQt5 库来创建 GUI 程序,并通过 QDateTime 类获取实时日期时间信息。其中,`update_time` 方法用于获取当前日期时间并更新标签。定时器每秒都会调用 `update_time` 方法,以实现实时更新时间的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值