【问题标题】:QDateTime custom format escaping symbolsQDateTime 自定义格式转义符号
【发布时间】:2019-09-04 11:49:24
【问题描述】:

我想获得一个自定义的QDateTime,例如:

QString string = "23 April 2012 at 22:51";
QString format = "d MMMM yyyy at hh:mm";

我无法做到,因为文字 at 未被识别为“附加”字符串,但具有关联的标记。

  • a -> 上午或下午
  • t -> 时区信息。

我自然会采取另一种方法:

QDateTime timeNow = QDateTime::currentDateTime();

QString time1Format = "d MMMM yyyy";
QString time2Format = "hh:mm";

QString time1 = timeNow.toString(time1Format);
QString time2 = timeNow.toString(time2Format);

QString timeConcat = QString(time1 + " at " + time2);

qDebug() << "Time = " << timeConcat;

如何转义格式中的“at”关键字?

ap 或解释为 AM/PM 时间。 ap 必须是“am”或“pm”。

【问题讨论】:

    标签: qt qdatetime


    【解决方案1】:

    您必须将at 字符串括在单引号内:

    将包含用单引号括起来的任何字符序列 输出字符串中的逐字记录(去掉引号),即使它 包含格式化字符。两个连续的单引号 ("''") 在输出中被单引号替换。中的所有其他字符 格式字符串逐字包含在输出字符串中。

    qDebug() << "Time = " << QDateTime::currentDateTime().toString("d MMMM yyyy 'at' hh::mm");
    

    【讨论】:

      猜你喜欢
      • 2013-04-22
      • 2019-01-29
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      相关资源
      最近更新 更多