PyQt6 中文教程2-日期和时间

QDate, QTime, QDateTime

PyQt6 有 QDateQDateTimeQTime 类处理日期和时间。QDate 是用于处理公历中的日期的类。它有获取、比较或操作日期的方法。QTime 类用于处理时间。它提供了比较时间、确定时间和其他各种时间操作方法。QDateTime 是 QDate 和 QTime 的组合。

PyQt 当前日期和时间

PyQt6 有 currentDatecurrentTime 和 currentDateTime 方法获取当前的日期或时间。

#pyqt6获取当前的日期或时间。
from PyQt6.QtCore import QDate, QTime, QDateTime, Qt
now = QDate.currentDate()       #获取系统日期
print(now.toString(Qt.DateFormat.ISODate))  #输出国际标准化组织,日期
print(now.toString(Qt.DateFormat.RFC2822Date))  #输出电子邮件日期和时间表示的格式
datetime = QDateTime.currentDateTime()  #获取系统时间和日期
print(datetime.toString())  #输入字符串格式的系统时间和日期。
time = QTime.currentTime()      #获取系统时间
print(time.toString(Qt.DateFormat.ISODate))     #输入国际标准化组织,时间

currentDate 方法返回当前的日期。

currentDateTime 方法返回当前的日期和时间。

currentTime 方法返回了当前时间。

 

运行结果:

2023-06-07
07 Jun 2023
Wed Jun 7 10:18:04 2023
10:18:04

PyQt6 UTC 时间

我们的星球是一个球体,绕着它自己的轴旋转。地球向东旋转,所以太阳在不同的时间在不同的地点升起。地球大约每24小时自转一次。因此,世界被划分为24个时区。在每个时区,都有不同的当地时间。当地时间通常会被夏时制进一步修改。

实际上也需要一个标准时间。一个标准时间有助于避免时区和夏令时的混淆。选择UTC(通用协调时间)作为主要的时间标准。UTC时间用于航空、天气预报、飞行计划、空中交通管制许可和地图。与当地时间不同,UTC时间不随季节变化而变化。

from PyQt6.QtCore import QDateTime, Qt
now = QDateTime.currentDateTime()   #返回了本地时间的当前时间
print('Local datetime: ', now.toString(Qt.DateFormat.ISODate))
print('Universal datetime: ', now.toUTC().toString(Qt.DateFormat.ISODate))  #toUTC 方法从时间对象里获取了标准时间。
print(f'The offset from UTC is: {now.offsetFromUtc()} seconds') #offsetFromUtc 方法给出了本地时间与标准时间的差,以秒为单位。
print('Local datetime: ', now.toString(Qt.DateFormat.ISODate))

我们使用 toUTC 方法从时间对象里获取了标准时间。

offsetFromUtc 方法给出了本地时间与标准时间的差,以秒为单位。

运行结果:Local datetime:  2023-06-07T11:18:48
Universal datetime:  2023-06-07T03:18:48Z
The offset from UTC is: 28800 seconds
Local datetime:  2023-06-07T11:18:48

PyQt6 天数

daysInMonth 方法返回了指定月份的天数,daysInYear 方法返回了指定年份的天数。

from PyQt6.QtCore import QDate
now = QDate.currentDate()
d = QDate(2023, 5, 7)
print(f'Days in month: {d.daysInMonth()}')
print(f'Days in year: {d.daysInYear()}')

本例打印了指定日期(2023-5-7)年份和月份的天数。

运行结果:

Days in month: 31
Days in year: 365

PyQt6 天数差

daysTo 方法返回了一个日期到另外一个日期的差。

from PyQt6.QtCore import QDate, Qt
now = QDate.currentDate()
y = now.year()
print(f'today is {now.toString(Qt.DateFormat.ISODate)}')    #输出今天的日期(标准格式)
xmas1 = QDate(y-1, 12, 25)  #设定变量为前年的圣诞节
xmas2 = QDate(y, 12, 25)  #设定变量为今年年的圣诞节
dayspassed = xmas1.daysTo(now)  #今天距前年圣诞节的天数
print(f'{dayspassed} days have passed since last XMas')
nofdays = now.daysTo(xmas2)  #今天距下一个圣诞节的天数
print(f'There are {nofdays} days until next XMas')

运行结果:

today is 2023-06-07
164 days have passed since last XMas
There are 201 days until next XMas

PyQt6 时间的计算

我们经常需要对天,秒或者年进行加减等计算。

from PyQt6.QtCore import QDateTime, Qt
now = QDateTime.currentDateTime()
print(f'Today: {now.toString(Qt.DateFormat.ISODate)}')  
#输出今天的日期时间
print(f'Adding 12 days: {now.addDays(12).toString(Qt.DateFormat.ISODate)}')  
#输出12天后的日期和时间
print(f'Subtracting 22 days: {now.addDays(-22).toString(Qt.DateFormat.ISODate)}')  
#输出22天前的日期和时间
print(f'Adding 50 seconds: {now.addSecs(50).toString(Qt.DateFormat.ISODate)}')  
#输出50秒后的日期和时间
print(f'Adding 3 months: {now.addMonths(3).toString(Qt.DateFormat.ISODate)}')  
#输出3月之后的日期和时间
print(f'Adding 12 years: {now.addYears(12).toString(Qt.DateFormat.ISODate)}')  
#输入12年之后的日期和时间

运行结果 :

Today: 2023-06-07T11:50:13
Adding 12 days: 2023-06-19T11:50:13
Subtracting 22 days: 2023-05-16T11:50:13
Adding 50 seconds: 2023-06-07T11:51:03
Adding 3 months: 2023-09-07T11:50:13
Adding 12 years: 2035-06-07T11:50:13

PyQt6 unix 纪元

纪元是被选为特定纪元起源的时间瞬间。 例如,在西方基督教国家,时间纪元从耶稣诞生的第 0 天开始。 另一个例子是使用了十二年的法国共和历。 这个时代是共和时代的开始,1792 年 9 月 22 日宣布第一共和国成立,君主制也被废除。

计算机也有它的时代。 最受欢迎的时代之一是 Unix 时代。 Unix 纪元是 1970 年 1 月 1 日 UTC 时间 00:00:00(或 1970-01-01T00:00:00Z ISO 8601)。 计算机中的日期和时间是根据自该计算机或平台定义的纪元以来经过的秒数或时钟滴答数确定的。

Unix 时间是自 Unix 纪元以来经过的秒数。

在 PyQt6 教程的这一部分中,我们使用了日期和时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值