QDateTime 转QString格式
时间: 2023-02-11 14:16:46 浏览: 256
QDateTime 可以使用 toString() 函数转换为 QString。
```
QDateTime current_datetime = QDateTime::currentDateTime();
QString current_datetime_string = current_datetime.toString("yyyy-MM-dd hh:mm:ss");
```
其中,"yyyy-MM-dd hh:mm:ss" 是格式化字符串,可以按照需要自定义。
可选的格式化字符有:
- d 日 (1~31)
- dd 日 (01~31)
- ddd 三个字母缩写星期几
- dddd 星期几
- M 月 (1~12)
- MM 月 (01~12)
- MMM 三个字母缩写月份
- MMMM 月份
- yy 年 (00~99)
- yyyy 年
- h 时 (1~12)
- hh 时 (01~12)
- H 时 (0~23)
- HH 时 (00~23)
- m 分 (0~59)
- mm 分 (00~59)
- s 秒 (0~59)
- ss 秒 (00~59)
- z 毫秒 (0~999)
- zzz 毫秒 (000~999)
- AP 或 ap AM/PM
- ap 上午/下午
在格式化字符串中,除了这些字符外的其它字符会被直接插入到输出中。
示例
QDateTime current_datetime = QDateTime::currentDateTime();
QString current_datetime_string = current_datetime.toString("yyyy年MM月dd日 dddd hh:mm:ss");
//2022年05月12日 星期四 12:23:34
相关推荐
![dat](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)