• 7338阅读
  • 7回复

日期格式问题【已解决】 [复制链接]

上一主题 下一主题
离线david1024
 
只看楼主 倒序阅读 楼主  发表于: 2010-06-23
— 本帖被 xtfllbl 执行合并操作(2010-06-23) —
请问日期如何format成如下格式:

Wed, 13 Jan 2010 15:36:23 GMT

谢谢!
[ 此帖被david1024在2010-06-23 18:50重新编辑 ]
菜鸟来学习
离线david1024
只看该作者 1楼 发表于: 2010-06-23
日期格式问题
请问日期如何format成如下格式:

Wed, 13 Jan 2010 15:36:23 GMT

谢谢!


菜鸟来学习
离线xtfllbl

只看该作者 2楼 发表于: 2010-06-23

QString QDateTime::toString ( const QString & format ) const
Returns the datetime as a string. The format parameter determines the format of the result string.
These expressions may be used for the date:
ExpressionOutput
dthe day as number without a leading zero (1 to 31)
ddthe day as number with a leading zero (01 to 31)
dddthe abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName().
ddddthe long localized day name (e.g. 'Monday' to 'Qt::Sunday'). Uses QDate::longDayName().
Mthe month as number without a leading zero (1-12)
MMthe month as number with a leading zero (01-12)
MMMthe abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName().
MMMMthe long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName().
yythe year as two digit number (00-99)
yyyythe year as four digit number

These expressions may be used for the time:
ExpressionOutput
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe milliseconds with leading zeroes (000 to 999)
APuse AM/PM display. AP will be replaced by either "AM" or "PM".
apuse am/pm display. ap will be replaced by either "am" or "pm".

All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Two consecutive singlequotes ("''") are replaced by a singlequote in the output.
Example format strings (assumed that the QDateTime is 21 May 2001 14:13:09):
FormatResult
dd.MM.yyyy21.05.2001
ddd MMMM d yyTue May 21 01
hh:mm:ss.zzz14:13:09.042
h:m:s ap2:13:9 pm

If the datetime is invalid, an empty string will be returned.


上海欢迎您
离线david1024
只看该作者 3楼 发表于: 2010-06-23
回 2楼(xtfllbl) 的帖子
谢谢您的回复,

我在format时用MMM,结果是“一月”,并不是“Jan”,这个语言在哪里设置?
菜鸟来学习
离线xtfllbl

只看该作者 4楼 发表于: 2010-06-23
回 3楼(david1024) 的帖子
很遗憾,您需要改变系统语言为英语,因为qdatetime读取的是系统接口时间,所以。。
要是怕麻烦,显示数字月份又何妨呢?
上海欢迎您
离线david1024
只看该作者 5楼 发表于: 2010-06-23
回 4楼(xtfllbl) 的帖子
向您具体介绍下我的问题:

我接收英文网站的信息,其中有日期、时间等信息,例如:Wed, 13 Jan 2010 15:36:23 GMT,我现在要把它改成13 Jan 15:36

我想用QDateTime::fromString("Wed, 13 Jan 2010 15:36:23 GMT", Format1);

QString afterFormat = QDateTime::toString(Format2);

Format1、Format2中的“MM”使结果都变成了中文,所以……
菜鸟来学习
离线dbzhang800

只看该作者 6楼 发表于: 2010-06-23
Re:回 2楼(xtfllbl) 的帖子
引用第3楼david1024于2010-06-23 16:47发表的 回 2楼(xtfllbl) 的帖子 :
谢谢您的回复,
我在format时用MMM,结果是“一月”,并不是“Jan”,这个语言在哪里设置?


  1. QString QLocale::toString ( const QDateTime & dateTime, const QString & format ) const
离线david1024
只看该作者 7楼 发表于: 2010-06-23
回 6楼(dbzhang800) 的帖子
谢谢您的提醒,问题解决了。

QLocale local(QLocale::English, QLocale::AmericanSamoa);

QDateTime dt = local.toDateTime("Wed, 13 Jan 2010 15:36:23","ddd, dd MMM yyyy hh:mm:ss");
菜鸟来学习
快速回复
限100 字节
 
上一个 下一个