Qt获取当前时间日期、设置时间日期

void DialogTest::slotGetCurrentTime()
{
	QDateTime currentDateTime = QDateTime::currentDateTime();
	ui.timeEdit->setTime(currentDateTime.time());
	ui.dateEdit->setDate(currentDateTime.date());
	ui.dateTimeEdit->setDateTime(currentDateTime);

	ui.time->setText(currentDateTime.toString("hh:mm:ss"));
	ui.date->setText(currentDateTime.toString("yyyy-MM-dd"));
	ui.timedate->setText(currentDateTime.toString("yyyy-MM-dd hh:mm:ss"));
}
void DialogTest::slotSetTime()
{
	QString str = ui.time->text();
	QTime tm = QTime::fromString(str,"hh:mm:ss");
	ui.timeEdit->setTime(tm);
}

void DialogTest::slotSetDate()
{
	QString str = ui.date->text();
	QDate date = QDate::fromString(str,"yyyy-MM-dd");
	ui.dateEdit->setDate(date);
}

void DialogTest::slotSetDateTime()
{
	QString str = ui.timedate->text();
	str = str.trimmed();//去掉空格
	QDateTime dt = QDateTime::fromString(str,"yyyy-MM-dd hh:mm:ss");
	ui.dateTimeEdit->setDateTime(dt);
}

你可能感兴趣的:(qt,开发语言)