Qt中获取系统时间可以用类QDateTime的一个静态函数,具体代码如下所示:
#include <QDateTime>
qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz dddd"); //年-月-日 时:分:秒.毫秒 星期五
其中static QDateTime currentDateTime();是一个静态函数,可以不用实例类对象直接调用。该函数返回值为QDateTime类型,可以直接转换为QString,然后格式化输出。
yyyy:年
hh:小时
mm:分钟
zzz:毫秒
dddd:星期五(ddd:周五)
参考内容:
https://blog.csdn.net/lusirking/article/details/51460716
(参考:QDateTime获取系统当前日期和时间)
Qt中获取系统时间可以用类QDateTime的一个静态函数,具体代码如下所示:#include <QDateTime>qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz dddd"); //年-月-日 时:分:秒.毫秒 星期五其中static QDateTime c...
#include
QDateTime
current_date_time =
QDateTime
::currentDateTime();
QString cdt = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
QT
ime current_time =
QT
ime::currentTime();
QDateTime
time =
QDateTime
::currentDateTime();
qint64 val = time.toMSecsSinceEpoch(); // 转换成毫秒的长整型
QDateTime
time2 =
QDateTime
::fromMSecsSinceEpoch(val); // 从长整型转化为Qdatatime
time2.toString("yyyy/ddd MMMM d yy"); // 指定
时间
格式
有的只需要大致
时间
,但有的需要精确到毫秒。集
中
罗列几种常用的
时间
具体
获取
方式。用
QDateTime
::currentDateTime()函数
获取
当前
时间
,用
QT
ime::currentTime()函数
通过调用
QT
ime::currentTime()函数,通过访问结构体内小时、分钟、秒钟、毫秒的方式,实现分布输出。
方法三:用打log的方式
写一个实现同步记录log的类对象,来对当前程序
中
的进度进行
具体
时间
和内容的输出。
特点:时...
QT
ime:
时间
数据类型,表示
时间
,如:14:23:13。
QDate:
日期
数据类型,表示
日期
,如:2020-4-5。
QDateTime
:
日期
时间
数据类型,表示
日期
和
时间
,如:2020-03-23 08:12:43。
QDateTime
:
QDateTime
dateTime=
QDateTime
::currentDateTime();//
获取
系统
当前的
时间
QString str = dateTime .toString("yyyy-MM-dd hh:mm:ss");//
QDateTime
dateTime;
QString strDateTime = dateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
// 从字符串转换为毫秒(需完整的年月日时分秒)
datetime.fromString("2018-07-18 12:10:10:111", "yyyy-MM-dd-
hh:mm:ss....
QT
毫秒级别字符串在转
日期
时间
格式时,要注意字符串的格式为2021-07-01 15:12:33.134,秒和毫秒之间要使用小数点,而不是使用冒号。
// ms 级字符串转换为
日期
时间
QDateTime
dd =
QDateTime
::fromString("2021-07-01 15:12:33.134","yyyy-MM-dd hh:mm:ss.zzz");
// 输出
日期
时间
qDebug() << dd;
// 转换成
时间
戳
qint64
2、显示当前
时间
ui->label->setText(“
QDateTime
:\nCurrent Time is:” +
QDateTime
::currentDateTime().toString(“yyyy-MM-dd hh:mm:ss ddd”));
ui->label->setText(ui->label->text() + "\n\n
QT
ime:\nHour minute second msec is:" +
使用
QT
中
的
QDateTime
时,
获取
当前的
星期
,一般的:
在
系统
语言是English下:
QDateTime
::currentDateTime().toString(“ddd”);//这样
获取
到的是Mon-Sun,英文
星期
的3个字母的缩写。
QDateTime
::currentDateTime().toString(“dddd”);//这样
获取
到的是Monday-Sunday,英文
星期
。
在
Qt
中
可以使用`
QDateTime
`类来
获取
当前
系统
时间
,其
中
`
QDateTime
::currentMSecsSinceEpoch()`函数可以返回当前
时间
距离Unix纪元以毫秒为单位的
时间
戳。示例代码如下:
```cpp
#include <
QDateTime
>
#include <QDebug>
int main(int argc, char *argv[])
qint64 currentMs =
QDateTime
::currentMSecsSinceEpoch();
qDebug() << "Current time in milliseconds since Unix Epoch:" << currentMs;
return 0;