QDateTime 与时区隔离?

问题描述 投票:0回答:1

我正在寻找来自 qt 的日期时间,以将字符串作为 isodate 返回给我,但带有时区。 我有时在网上查找我的问题,但没有找到解决方案

我刚刚得到这个:

this->ui.dateEnd->dateTime().toString(Qt::ISODate);

给我这个:

1900-10-31T23:00:00Z

或者还有这个:

this->ui.dateEnd->dateTime().toUfc().toString(Qt::ISODate);

给我这个:

1900-10-31T23:00:00Z

我想要这个:

1900-10-31T23:00:00+01.00.00

如果有人有想法,谢谢!

c++ qt datetime timezone
1个回答
5
投票

解决了我在评论中提到的错误:

QDateTime local = QDateTime::currentDateTime();
QDateTime utc = local.toUTC();
utc.setTimeSpec(Qt::LocalTime);

qint64 const utcOffset = utc.secsTo(local);

qDebug() << local.toString(Qt::ISODate);
qDebug() << utc.toString(Qt::ISODate);
qDebug() << utcOffset;

local.setUtcOffset(utcOffset);
qDebug() << local.toString(Qt::ISODate);

输出:

"2013-09-12T00:17:39"  
"2013-09-11T21:17:39"  
10800 
"2013-09-12T00:17:39+03:00"
© www.soinside.com 2019 - 2024. All rights reserved.