本文翻译及代码来自zetcode.com
PyQt5中有QDate、QDateTime、QTime等类来处理时间和日期。
QDate is a class for working with a calender date in Gregorian calendar.
It has methods for determining the date, comparing, or manipulating dates.
QTime class works with a clock time.
It provides methods for comparing time, determining the time and various other time manipulating methods.
QDateTime is a class that combines both QDate and QTime objects into one object.
Current date and time
PyQt5 中 currentdDate(), currentTime()和currentDateTime()方法来表示当前日期和时间。
from PyQt5.QtCore import QDate, QTime, QDateTime, Qt
now = QDate.currentDate() # returns the current date
print(now.toString(Qt.ISODate))
print(now.toString(Qt.DefaultLocaleLongDate))
datetime = QDateTime.currentDateTime() # returns the current date and time
print(datetime.toString())
time = QTime.currentTime() # returns the current time
print(time.toString(Qt.DefaultLocaleLongDate))
其中:
print(now.toString(Qt.ISODate))
print(now.toString(Qt.DefaultLocaleLongDate))
Qt.ISODate和Qt.DefaultLocalLongDate将值传递给toString()方法,打印出两种不同格式的日期
下面为上述代码的输出:
2018-06-29
2018年6月29日星期五
周五 6月 29 21:03:54 2018
CST 下午9:03:54
UTC time
协调世界时,又称世界统一时间、世界标准时间、国际协调时间,简称UTC。
网络时间协议就是协调世界时在互联网中使用的一种方式
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()))
currentDatetime()方法返回当前本地的日期和时间
toLocalTime()方法可以将世界时间转换为本地时间
toUTC()方法得到世界时间
offsetFromUtc()给出本地时间和世界时间的差值
以下是上述代码的输出:
Local datetime:: 2018-06-29T23:01:38
Universal datetime: 2018-06-29T15:01:38Z
The offset from UTC is : 28800 seconds
Number of days
daysInMonth()方法返回日期所在月份的天数
daysInYear()方法返回日期所在年份的天数
dayOfWeek()方法返回日期所在月份的第几周
dayOfYear()方法返回日期所在的年份的第几天
from PyQt5.QtCore import QDate, Qt
now = QDate.currentDate()
d = QDate(2018, 1, 17)
print("Days in month: {0}".format(d.daysInMonth()))
print("Day of week: {0}".format(d.dayOfWeek()))
print("Days in year: {0}".format(d.daysInYear()))
print("Day of year: {0}".format(d.dayOfYear()))
以下是输出结果:
Days in month: 31
Day of week: 3
Days in year: 365
Day of year: 17
Difference in days
daysTo()方法返回两个日期的差值
from PyQt5.QtCore import QDate
mDay1 = QDate(2018, 1, 17)
mDay2 = QDate(2018, 6, 30)
now = QDate.currentDate()
dayspassed =mDay1.daysTo(now)
print("{0} days have passed since last Memorial Day".format(dayspassed))
nofdays = now.daysTo(mDay2)
print("There are {0} day until next Memorial Day".format(nofdays))
以下是代码的输出:
163 days have passed since last Memorial Day
There are 1 day until next Memorial Day
Datetime arithmetic
日期算术
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 months: {0}".format(now.addMonths(3).toString(Qt.ISODate)))
print("Adding 12 years: {0}".format(now.addYears(12).toString(Qt.ISODate)))
以下是输出:
Today: 2018-06-30T00:10:31
Adding 12 days: 2018-07-12T00:10:31
Subtracting 22 days: 2018-06-08T00:10:31
Adding 50 seconds: 2018-06-30T00:11:21
Adding 3 months: 2018-09-30T00:10:31
Adding 12 years: 2030-06-30T00:10:31