QTime類為用戶提供一系列時間的函數,封裝的很全面,幾乎滿足了各種時間的需求。
常用方法介紹
1.QTime addMSecs(int ms) const
當前時間增加毫秒,ms可為負
2.QTime addSecs(int s) const
當前時間增加秒,s可為負
3.int elapsed() const
返回自上次調用start()或restart()以來經過的毫秒數。
4.int hour() const
返回小時數
5.int minute() const
返回分鍾數
6.second() const
返回秒數
7.int msec() const
返回毫秒數
8.bool isNull() const
如果時間為空返回true
9.bool isValid() const
判斷當前時間對象是否有效,比如H的范圍是0~23,M和S的范圍是0~59
10.int msecsSinceStartOfDay() const
返回從一天開始的秒數,即從00:00:00開始的秒數。
11.int secsTo(const QTime &t) const
返回從當前時間到t的秒數。如果t比這個時間早,返回的毫秒數為負。
12.int msecsTo(const QTime &t) const
返回從當前時間到t的毫秒數。如果t比這個時間早,返回的毫秒數為負。
13.int restart()int
將此時間設置為當前時間,並返回自上次調用start()或restart()以來經過的毫秒數。
14.bool setHMS(int h, int m, int s, int ms = 0)
將時間設置為小時h、分鍾m、秒s和毫秒ms。
15.void start()
將當前系統時間記錄為當前時間
16.QString toString(const QString &format) const
將時間轉化為特定的字符串格式
17.QString toString(Qt::DateFormat format = Qt::TextDate) const
按照Qt::DateFormat的格式轉化
18.QTime currentTime()
獲得系統當前時間
19.QTime fromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
從Qt::DateFormat轉化為QTime對象
20.QTime fromString(const QString &string, const QString &format)
從特定的字符串格式轉化為QTime對象
時間字符串格式
h:沒有補零的小時(如果AM/PM顯示,則為0到23或1到12)
hh:位數不夠需要補零的小時(00至23或01至12,如果是AM/PM顯示)
m:沒有補零的分鍾(0到59)
mm:位數不夠需要補零的分鍾(00到59)
s:沒有補零的秒(0到59)
ss:位數不夠需要補零的秒(00到59)
z:沒有補零的毫秒(0到999)
zzz:位數不夠需要補零的毫秒(000到999)
ap/AP:上午/下午,ap為am或pm,AP為AM或PM
例:
hh:mm:ss.zzz 08:18:68.138
h:m:s ap 8:18:68 am