PyQt5日期与时间 QDate, QTime, QDateTime

Python 4071 0 2020-12-31

PyQt5日期与时间 QDate, QTime, QDateTime

PyQt5用于处理日期和时间的类有QDate,QDateTime,QTime。

QDate类用于处理公历日期。它提供的方法可以测定日期、比较或者操作日期。

QTime类用于处理时间。它提供的方法可以测定时间、比较或者多样化操作时间。

QDateTime类将QDate对象和QTime对象整合为一个对象。


当前日期和时间

PyQt5 用于测定当前日期和时间的方法有 currentDate(), currentTime() 和currentDateTime() .

实例及注释:


#文件名current_date_time.py

from PyQt5.QtCore import QDate, QTime, QDateTime, Qt

now = QDate.currentDate()#获取当前日期

print(now.toString(Qt.ISODate)) #ISO日期格式打印

print(now.toString(Qt.DefaultLocaleLongDate)) #本地化长格式日期打印

datetime = QDateTime.currentDateTime() #获取当前日期与时间

print(datetime.toString()) #当前日期与时间打印

time = QTime.currentTime() #获取当前时间

print(time.toString(Qt.DefaultLocaleLongDate)) #本地化长格式时间打印


运行上面的文件,输出结果如下:

2018-01-14

2018年1月14日

周日 1月 14 17:00:34 2018

17:00:34


UTC(Universal Time Coordinated)协调世界时

全球有24个时区,每个时区有不同的本地时间。而且有时这些本地时间还是经过进一步修改的夏令时。为了避免这些时间相互混淆,就需要一个世界标准时间。UTC即是选出的主要标准。

实例及注释:


#文件名 utc_local.py

from PyQt5.QtCore import QDateTime, Qt

now = QDateTime.currentDateTime() #获取当前日期与时间

print("Local datetime: ", now.toString(Qt.ISODate)) #本地时间ISO日期格式打印

print("Universal datetime: ", now.toUTC().toString(Qt.ISODate)) #转化成UTC日期与时间后打印

print("The offset from UTC is: {0} seconds".format(now.offsetFromUtc())) #当前时间与UTC之间的偏移量


输出结果如下:

Local datetime: 2018-01-14T17:10:23

Universal datetime: 2018-01-14T09:10:23Z

The offset from UTC is: 28800 seconds


总天数

特定日期所在月份或所在年份的总天数。


#文件名: days.py

from PyQt5.QtCore import QDate, Qt

d = QDate(1945, 5, 7) #设定目标日期

print("Days in month: {0}".format(d.daysInMonth())) #目标日期所在月份的总天数

print("Days in year: {0}".format(d.daysInYear())) #目标日期所在年份的总天数


输出结果如下:

Days in month: 31

Days in year: 365


间隔天数


#文件名 xmas.py

from PyQt5.QtCore import QDate

xmas1 = QDate(2017, 12, 24) #上一个圣诞节日期

xmas2 = QDate(2018, 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))


输出结果如下:

21 days have passed since last XMas

There are 344 days until next XMas


日期与时间计算


#文件名 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)))#当前日期与时间加上12天

print("Subtracting 22 days: {0}".format(now.addDays(-22).toString(Qt.ISODate)))#当前日期与时间减去22天

print("Adding 50 seconds: {0}".format(now.addSecs(50).toString(Qt.ISODate)))#当前日期与时间加上50秒

print("Adding 3 months: {0}".format(now.addMonths(3).toString(Qt.ISODate)))#当前日期与时间加上3个月

print("Adding 12 years: {0}".format(now.addYears(12).toString(Qt.ISODate)))#当前日期与时间加上12年


输出结果如下:

Today: 2018-01-14T17:23:57

Adding 12 days: 2018-01-26T17:23:57

Subtracting 22 days: 2017-12-23T17:23:57

Adding 50 seconds: 2018-01-14T17:24:47

Adding 3 months: 2018-04-14T17:23:57

Adding 12 years: 2030-01-14T17:23:57


夏令时 daylight saving time (DST)


#文件名 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")


输出结果如下:

Time zone: 中国标准时间

The current date does not fall into DST time


Unix时间戳

时间戳是选来用于当作特定时代起点的一个瞬间。比如西方基督教国家的时间戳开始于耶稣降生日。再比如使用过20年的法国共和历,它的起点是1792年9月22日,第一共和国宣布成立,君主制被废除。

电脑也有它们的时间戳。最流行的是Unix时间戳,它的起点是1970年1月1日 UTC 时间 00:00:00。电脑中的日期与时间就是通过测定距离时间戳的秒数或者时钟滴答数来计算的。

Unix时间是距离Unix时间戳的秒数。


#文件名 unix_time.py

from PyQt5.QtCore import QDateTime, Qt

now = QDateTime.currentDateTime() #当前日期与时间

unix_time = now.toSecsSinceEpoch() #获取Unix时间

print(unix_time)

d = QDateTime.fromSecsSinceEpoch(unix_time) #Unix时间转换为QDate

print(d.toString(Qt.ISODate))


输出结果如下:

1515923173

2018-01-14T17:46:13


注:原文下面还有介绍儒略日(Julian Day),多为天文学家采用,貌似用不到。


上一篇:pyqt5 对话框的封装调用

下一篇:pip install 时报错 [WinError 5] 拒绝访问

讨论数量:1

天涯网魂 3 杠 5 星2020-12-31 21:04:39

now=QDate.currentDate()

date=now.toString("yyyy-MM-dd")  #当前日期

year=now.toString("yyyy")  #当前年份

month=now.toString("MM")  #当前月份

day=now.toString("dd")  #当前天


QDate.fromString(date,"yyyy-MM-dd")  #转化为时间类型

请先登录再发表讨论。 2024-04-28

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链