pyqt - QDate,QTime,QdateTime

Posted 沧海一粒水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt - QDate,QTime,QdateTime相关的知识,希望对你有一定的参考价值。

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))
‘‘‘2018-01-04
2018年1月4日
周四 1月 4 23:08:52 2018
23:08:52
‘‘‘
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()))
‘‘‘
Local datetime: 2018-01-04T23:08:52
Universal datetime: 2018-01-04T15:08:52Z
The offset from UTC is: 28800 seconds
‘‘‘
now = QDate.currentDate()

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
‘‘‘

xmas1 = QDate(2016, 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))
# 376 days have passed since last XMas
# There are 354 days until next XMas

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 months: {0}".format(now.addMonths(3).toString(Qt.ISODate)))
print("Adding 12 years: {0}".format(now.addYears(12).toString(Qt.ISODate)))
‘‘‘
Today: 2018-01-04T23:08:52
Adding 12 days: 2018-01-16T23:08:52
Subtracting 22 days: 2017-12-13T23:08:52
Adding 50 seconds: 2018-01-04T23:09:42
Adding 3 months: 2018-04-04T23:08:52
Adding 12 years: 2030-01-04T23:08:52
‘‘‘
now = QDateTime.currentDateTime()

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

if now.isDaylightTime():
print("The current date falls into DST time")#dst 夏令时
else:
print("The current does not fall into DST time")
‘‘‘
Time zone: 中国标准时间
The current date does not fall into DST time
‘‘‘
now = QDateTime.currentDateTime()

unix_time = now.toSecsSinceEpoch()
print(unix_time)

d = QDateTime.fromSecsSinceEpoch(unix_time)
print(d.toString(Qt.ISODate))
‘‘‘
1515078532自从1970年过的秒数
2018-01-04T23:08:52
‘‘‘

以上是关于pyqt - QDate,QTime,QdateTime的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5的日期和时间

pyqt 设置浏览器时区

qt tablewidget 怎么自己增加行

2.6.2 QDate类与QDateEdit控件介绍(日期处理)

从 int 或 string 创建 Qdate 对象

如何将 qdate 添加到 qtableview