QT中获取时间和日期的主要是 QTime 、QDate 和 QDateTime 这三个类。
通过 QTime 类中提供的时间相关的方法,可以获取到当前系统时间(时、分、秒、毫秒),需要注意的是,计时的准确性由底层操作系统决定,并不是所有的操作系统都能精确到毫秒级别。
通过调用 QTime 类中的 currentTime() 方法可以获取到当前系统时间:
QTime time = QTime::currentTime();
qDebug() << time;
输出结果:
QTime("12:01:13.427")
如果我们需要获取字符串形式的时间,可以使用 toString() 这个方法:
QTime time = QTime::currentTime();
qDebug() << time.toString("hh:mm:ss");
输出结果:
"12:01:13"
字符串形式的时间输出格式由 toString() 方法中的 format 参数列表决定,可用的参数列表如下:

如果我们在显示时间的同时还需要显示上午或下午,可以在格式列表添加添加 “AP、A、ap、a” 等选项:
QTime time = QTime::currentTime();
qDebug() << time.toString("hh:mm:ss a");
输出结果:
"02:29:31 下午"
当你电脑的系统语言使用中文时,不管格式列表中填 AP、A、ap、a 这四个选项里的哪一个,都只会显示上午或下午;只有当电脑系统语言使用英文时才会区分大小写,例如选择 AP/A,显示 AM/PM,选择 ap/a,显示 am/pm 。
hh字段的显示格式受 AP/A 或 ap/a 影响,如果格式列表中使用了 AP/A 或 ap/a 选项区分上下午,则 hh字段采用12小时制格式显示;否则使用24小时制格式显示:
QTime time = QTime::currentTime();
qDebug() << time.toString("hh:mm:ss a");
qDebug() << time.toString("hh:mm:ss");
输出结果:
"02:50:38 下午"
"14:50:38"
HH字段的显示格式则不受 AP/A 或 ap/a 影响,不管格式列表中是否使用 AP/A 或 ap/a 选项区分上下午,HH字段均采用24小时制格式显示:
QTime time = QTime::currentTime();
qDebug() << time.toString("HH:mm:ss a");
qDebug() << time.toString("HH:mm:ss");
输出结果:
"14:52:03 下午"
"14:52:03"
在格式列表中添加 t 选项可以用来获取时区信息:
QTime time = QTime::currentTime();
qDebug() << time.toString("hh:mm:ss t");
输出结果:
"14:59:02 中国标准时间"
修改时区后输出结果:
"14:00:45 新西伯利亚标准时间"
通过调用 QDate 类中的 currentDate() 方法可以获取到当前系统日期:
QDate date = QDate::currentDate();
qDebug() << date;
qDebug() << date.toString("yyyy-MM-dd");
输出结果:
QDate("2022-04-29")
"2022-04-29"
QDate类中对日期的操作与QTime类中对时间的操作基本一样,需要字符串格式的日期时,使用 toString() 方法即可,QDate类中对日期操作常用格式如下:

需要显示星期时,使用 ddd 或 dddd 选项:
QDate date = QDate::currentDate();
qDebug() << date;
qDebug() << date.toString("yyyy-MM-dd ddd");
qDebug() << date.toString("yyyy-MM-dd dddd");
输出结果:
"2022-04-29 周五"
"2022-04-29 星期五"
QDateTime类是 QDate 和 QTime 的组合,提供一系列时间和日期相关的函数。
通过调用 QDateTime 类中的 currentDateTime() 方法可以获取到当前系统时间和日期:
QDateTime dateTime;
dateTime = QDateTime::currentDateTime();
qDebug()<<dateTime;
qDebug() << dateTime.toString("yyyy-MM-dd hh:mm:ss ddd");
输出结果:
QDateTime(2022-04-29 15:22:23.615 中国标准时间 Qt::TimeSpec(LocalTime))
"2022-04-29 15:22:23 周五"
使用 toString() 方法将时间和日期转换成字符串形式时,格式与 QTime、QDate 中的格式一样。

创建一个定时器,每秒获取一次系统时间和日期,转换成字符串形式后再通过Label空间显示即可完整代码如下:
main.cpp
#include "dateTime.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DateTime w;
w.show();
return a.exec();
}
dateTime.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDateTime>
#include <QDebug>
#include <QTimer>
#include <QLabel>
#include <QVBoxLayout>
#include <QApplication>
class DateTime : public QWidget
{
Q_OBJECT
public:
DateTime(QWidget *parent = nullptr);
~DateTime();
void timeUpdate(void);
private:
QDateTime dateTime;
QTimer *timer;
QLabel *label;
};
#endif
dateTime.cpp
#include "dateTime.h"
DateTime::DateTime(QWidget *parent)
: QWidget(parent)
{
//设置窗口标题和窗口大小
this->setWindowTitle("时间更新显示例程");
this->resize(500, 100);
//创建label对象显示时间和日期
label = new QLabel(this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(label);
this->setLayout(layout);
//初始化时间和日期显示
dateTime = QDateTime::currentDateTime();
this->label->setText(dateTime.toString("yyyy-MM-dd hh:mm:ss ddd"));
//创建定时器定时更新时间和日期
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &DateTime::timeUpdate);
timer->start(1000);
}
DateTime::~DateTime()
{
delete timer;
}
void DateTime::timeUpdate(void)
{
dateTime = QDateTime::currentDateTime();
this->label->setText(dateTime.toString("yyyy-MM-dd hh:mm:ss ddd"));
}
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge