基础知识
- QDate 处理日期
- QTime 处理时间
- QDateTime 处理日期和时间
初始化
- 可以在实例化时直接传入参数初始化
- 可以再创建实例后通过 setDate() 和 setHMS 进行设置。它们的返回值类型为 bool,表示日期和时间是否创建成功
- toString() 方法中可以传入格式字符串
void initTimeAndDate()
{
// QDate 初始化
QDate dt1(2022, 9, 23);
qDebug() << "The date is" << dt1.toString();
QDate dt2;
dt2.setDate(2022, 9, 24);
qDebug() << "The date is" << dt2.toString();
// QTime 初始化
QTime tm1(17, 30, 12, 55); // 最后一位毫秒可以省略,默认为 0
qDebug() << "The time is" << tm1.toString("hh:mm:ss.zzz");
QTime tm2;
tm2.setHMS(13, 53, 23, 15); // 最后一位毫秒可以省略,默认为 0
qDebug() << "The time is" << tm2.toString("hh:mm:ss A");
}
当前日期和时间
QDate cd = QDate::currentDate();
QTime ct = QTime::currentTime();
void currentDateAndTime()
{
QDate cd = QDate::currentDate();
QTime ct = QTime::currentTime();
qDebug() << "Current date is" << cd.toString();
qDebug() << "Current time is" << ct.toString();
}
比较日期和时间
void compareDateAndTime()
{
QDate dt1(2020, 4, 5);
QDate dt2(2021, 5, 6);
if (dt1 < dt2)
{
qDebug() << dt1.toString() << "comes before" << dt2.toString();
}
else
{
qDebug() << dt1.toString() << "comes after" << dt2.toString();
}
}
判断闰年
QDate::isLeapYear( int year );
void leapYear()
{
QList<int> years = { 2012, 2013, 2014, 2015, 2016, 2020, 2024 };
for (int& year : years) {
if (QDate::isLeapYear(year))
{
qDebug() << year << "is a leap year";
}
else
{
qDebug() << year << "is not a leap year";
}
}
}
预定义和自定义格式
- 预定义格式用枚举,例如:
Qt::TextDate
- 自定义格式用格式字符串,例如:
"yyyy-MM-dd"
void predefinedAndUserDefinedFormat()
{
QDate cd = QDate::currentDate();
// 预定义格式
qDebug() << "Today is" << cd.toString();
qDebug() << "Today is" << cd.toString(Qt::TextDate);
qDebug() << "Today is" << cd.toString(Qt::ISODate);
qDebug() << "Today is" << cd.toString(Qt::RFC2822Date);
// 自定义格式
qDebug() << "Today is" << cd.toString("yyyy-MM-dd");
}
周工作日
#include <QLocale>
void weekday()
{
QDate cd = QDate::currentDate();
int wd = cd.dayOfWeek();
qDebug() << wd;
// 周工作日本地化显示
QLocale locale(QLocale::Chinese);
qDebug() << "今天是" << locale.dayName(wd);
qDebug() << "今天是" << locale.dayName(wd, QLocale::ShortFormat);
QLocale locale_eng(QLocale::English);
qDebug() << "Today is" << locale_eng.dayName(wd);
qDebug() << "Today is" << locale_eng.dayName(wd, QLocale::ShortFormat);
}
计算天数
dt1.daysInMonth()
dt1.daysInYear()
dt1.day()
dt1.month()
dt1.year()
void countDays()
{
QList<QString> months;
months.append("January");
months.append("Feburary");
months.append("March");
months.append("April");
months.append("May");
months.append("June");
months.append("July");
months.append("August");
months.append("September");
months.append("October");
months.append("November");
months.append("December");
QDate dt1(2020, 9, 18);
QDate dt2(2020, 2, 11);
QDate dt3(2020, 5, 1);
QDate dt4(2020, 12, 11);
QDate dt5(2020, 2, 29);
qDebug() << "There are" << dt1.daysInMonth() << "days in" << months.at(dt1.month() - 1);
qDebug() << "There are" << dt2.daysInMonth() << "days in" << months.at(dt2.month() - 1);
qDebug() << "There are" << dt3.daysInMonth() << "days in" << months.at(dt3.month() - 1);
qDebug() << "There are" << dt4.daysInMonth() << "days in" << months.at(dt4.month() - 1);
qDebug() << "There are" << dt5.daysInMonth() << "days in" << months.at(dt5.month() - 1);
qDebug() << "There are" << dt1.daysInYear() << "days in yar" << dt1.year();
}
检查日期有效性
void validDate()
{
QList<QDate> dates = {QDate(2020, 5, 11), QDate(2020, 8, 1), QDate(2020, 2, 30)};
for (QDate date : dates) {
if (date.isValid())
{
qDebug() << date.toString() << "is a valid date";
}
else
{
qDebug() << date.toString() << "is not a valid date";
}
}
}
日期做加减法
dt1.addDays( int days )
dt1.daysTo( QDate date )
void addMinusDays()
{
QDate dt(2022, 5, 11);
QDate newDt = dt.addDays(55);
QDate xmas(2022, 12, 24);
qDebug() << "55 days from" << dt.toString() << "is" << newDt.toString();
qDebug() << "There are" << QDate::currentDate().daysTo(xmas) << "days till Chrismas";
}
QDateTime
- 包含日期和时间
cdt1.toString()
cdt1.date()
cdt1.time()
void dateTime()
{
QDateTime cdt = QDateTime::currentDateTime();
qDebug() << "The current datetime is" << cdt.toString();
qDebug() << "The current date is" << cdt.date().toString();
qDebug() << "The current time is" << cdt.time().toString();
}
UNIX 新纪元(Epoch)
- 1970年1月1日08:00:00
time(nullptr); // 返回的是当前时间距新纪元的描述
dt1.setSecsSinceEpoch( int secs )
dt1.toSecsSinceEpoch()
void unixEpoch()
{
QDateTime dt;
dt.setSecsSinceEpoch(0);
qDebug() << dt.toString();
dt.setSecsSinceEpoch(time(nullptr));
qDebug() << dt.toString();
QDateTime cd = QDateTime::currentDateTime();
qDebug() << cd.toString();
qDebug() << time(nullptr);
qDebug() << cd.toSecsSinceEpoch();
}