您的位置:首页 > 教育 > 锐评 > 邯郸市都有哪些网络推广公司_小程序项目开发报价_职业培训网络平台_广州seo站内优化

邯郸市都有哪些网络推广公司_小程序项目开发报价_职业培训网络平台_广州seo站内优化

2025/4/18 11:59:27 来源:https://blog.csdn.net/qq_37529913/article/details/142749502  浏览:    关键词:邯郸市都有哪些网络推广公司_小程序项目开发报价_职业培训网络平台_广州seo站内优化
邯郸市都有哪些网络推广公司_小程序项目开发报价_职业培训网络平台_广州seo站内优化

        QDateTime 是 Qt 框架中用于处理日期和时间的类。本篇文章详细介绍、通过示例 快速了解QDateTime的各种操作,包括: 当前时间、获取日期和时间、获取日期、获取时间、获取时间戳、格式化输出、年、月、日、QTime时间、获取微妙、操作日期和时间、添加时间、减去时间、指定时间、比较时间、时区处理、设置时区、查询时区、常用时区、转换时区、有效性等操作

转载请附上文章出处与本文链接。

QDateTime 使用详解目录

1 当前时间

2 获取日期和时间

2.1 获取日期

2.2 获取时间

2.3 获取时间戳

3 格式化输出

3.1 字符串

3.2 年

3.3 月

3.4 日

3.5 其它

4 QTime

4.1 获取年月日

4.2 获取微妙

5 操作日期和时间

5.1 添加时间

5.2 减去时间

5.3 指定时间

6 比较时间

7 时区处理

7.1 设置时区

7.2 查询时区

7.3 常用时区

7.4 转换时区

8 有效性

9 .h源文件

10 .cpp


1 当前时间

	QDateTime currentDateTime = QDateTime::currentDateTime();qDebug() << currentDateTime;	//QDateTime(2024-10-08 01:36:56.886 中国标准时间 Qt::LocalTime)

2 获取日期和时间

2.1 获取日期

	QDate date = currentDateTime.date();qDebug() << date;QDate("2024-10-08")

2.2 获取时间

	QTime time = currentDateTime.time();qDebug() << time;QTime("01:50:30.786")

2.3 获取时间戳

	qint64 timestamp = currentDateTime.toMSecsSinceEpoch();qDebug() << timestamp;1728323430786

3 格式化输出

3.1 字符串

    QString formattedString = currentDateTime.toString("yyyy-MM-dd HH:mm:ss zzz");qDebug() << formattedString;"2024-10-08 01:54:59 638"

3.2 年

	//年formattedString = currentDateTime.toString("yyyy");qDebug() << formattedString;

3.3 月

	//月formattedString = currentDateTime.toString("MM");qDebug() << formattedString;

3.4 日

	//日formattedString = currentDateTime.toString("dd");qDebug() << formattedString;

3.5 其它

// 时、分、秒、毫秒 同上

4 QTime

4.1 获取年月日

	int year = currentDateTime.date().year();   // 获取年份int month = currentDateTime.date().month(); // 获取月份int day = currentDateTime.date().day();     // 获取日期qDebug() << "年:" << year << "月:" << month << "日:" << day;年: 2024 月: 10 日: 8

4.2 获取微妙

	QTime time = currentDateTime.time();int milliseconds = time.msec(); // 获取毫秒int microseconds = milliseconds * 1000; // 转换为微秒qDebug() << "微秒:" << microseconds;微秒: 244000

5 操作日期和时间

5.1 添加时间

	//添加时间:QDateTime futureDateTime = currentDateTime.addDays(5); // 添加5天qDebug() << futureDateTime;QDateTime(2024-10-13 02:07:11.244 中国标准时间 Qt::LocalTime)

5.2 减去时间

	//减去时间:QDateTime pastDateTime = currentDateTime.addMonths(-1); // 减去1个月qDebug() << pastDateTime;QDateTime(2024-09-08 02:07:11.244 中国标准时间 Qt::LocalTime)

5.3 指定时间

	QDateTime specificDateTime(QDate(2023, 10, 1), QTime(12, 30, 0));qDebug() << specificDateTime;	//QDateTime(2023-10-01 12:30:00.000 中国标准时间 Qt::LocalTime)

6 比较时间

	//比较两个 QDateTime 对象:if (currentDateTime < specificDateTime){// currentDateTime 早于 specificDateTime}

7 时区处理

7.1 设置时区

	QTimeZone timeZone("Asia/Shanghai");QDateTime dateTimeInZone = currentDateTime.toTimeZone(timeZone);

7.2 查询时区

    // 获取所有可用的时区 IDQStringList timeZoneIds = QTimeZone::availableTimeZoneIds();// 打印所有时区 IDforeach (const QString &id, timeZoneIds){qDebug() << id;}

7.3 常用时区

UTC:协调世界时Asia/Shanghai:中国标准时间America/New_York:东部标准时间Europe/London:格林威治标准时间Asia/Tokyo:日本标准时间

7.4 转换时区

QDateTime utcDateTime = currentDateTime.toUTC();

8 有效性

	//检查有效性:if (currentDateTime.isValid()){// 当前时间有效}

9 .h源文件

#pragma once#include <QtWidgets/QMainWindow>
#include "ui_QDateTimeTest.h"#include <QDebug>
#include <QDateTime>
#include <QTimeZone>
#pragma execution_character_set("utf-8")
class QDateTimeTest : public QMainWindow
{Q_OBJECTpublic:QDateTimeTest(QWidget *parent = nullptr);~QDateTimeTest();private:Ui::QDateTimeTestClass ui;
};

10 .cpp

#include "QDateTimeTest.h"QDateTimeTest::QDateTimeTest(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);QDateTime currentDateTime = QDateTime::currentDateTime();qDebug() << currentDateTime;	//QDateTime(2024-10-08 01:36:56.886 中国标准时间 Qt::LocalTime)///@ 获取日期和时间qDebug() << "获取日期和时间";//获取日期:QDate date = currentDateTime.date();qDebug() << date;//获取时间:QTime time = currentDateTime.time();qDebug() << time;//获取时间戳:qint64 timestamp = currentDateTime.toMSecsSinceEpoch();qDebug() << timestamp;///@ 格式化输出qDebug() << "格式化输出";//格式化为字符串:QString formattedString = currentDateTime.toString("yyyy-MM-dd HH:mm:ss zzz");qDebug() << formattedString;//年formattedString = currentDateTime.toString("yyyy");qDebug() << formattedString;//月formattedString = currentDateTime.toString("MM");qDebug() << formattedString;//日formattedString = currentDateTime.toString("dd");qDebug() << formattedString;// 时、分、秒、毫秒类似///@ QTimeint year = currentDateTime.date().year();   // 获取年份int month = currentDateTime.date().month(); // 获取月份int day = currentDateTime.date().day();     // 获取日期qDebug() << "年:" << year << "月:" << month << "日:" << day;time = currentDateTime.time();int milliseconds = time.msec(); // 获取毫秒int microseconds = milliseconds * 1000; // 转换为微秒qDebug() << "微秒:" << microseconds;///@ 操作日期和时间qDebug() << "操作日期和时间";//添加时间:QDateTime futureDateTime = currentDateTime.addDays(5); // 添加5天qDebug() << futureDateTime;//减去时间:QDateTime pastDateTime = currentDateTime.addMonths(-1); // 减去1个月qDebug() << pastDateTime;QDateTime specificDateTime(QDate(2023, 10, 1), QTime(12, 30, 0));qDebug() << specificDateTime;	//QDateTime(2023-10-01 12:30:00.000 中国标准时间 Qt::LocalTime)///@ 比较//比较两个 QDateTime 对象:if (currentDateTime < specificDateTime){// currentDateTime 早于 specificDateTime}///@ 时区处理//设置时区:QTimeZone timeZone("Asia/Shanghai");QDateTime dateTimeInZone = currentDateTime.toTimeZone(timeZone);//获取时区:QTimeZone currentZone = currentDateTime.timeZone();///@ 其他方法//检查有效性:if (currentDateTime.isValid()){// 当前时间有效}//转换为 UTC:QDateTime utcDateTime = currentDateTime.toUTC();}QDateTimeTest::~QDateTimeTest()
{}

11 其它文章

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com