c++ 将20230524172643字符串中提取年月日时分秒
时间: 2024-02-01 15:13:32 浏览: 44
可以使用C++标准库中的字符串处理函数来实现。可以先将字符串转换为time_t类型,然后再使用strftime函数进行格式化输出。
示例代码如下:
```c++
#include <iostream>
#include <string>
#include <ctime>
int main()
{
std::string str = "20230524172643";
// 将字符串转换为time_t类型
struct tm tm;
strptime(str.c_str(), "%Y%m%d%H%M%S", &tm);
time_t t = mktime(&tm);
// 格式化输出年月日时分秒
char buf[80];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t));
std::cout << buf << std::endl;
return 0;
}
```
输出结果为:
```
2023-05-24 17:26:43
```
相关问题
c++ 将日期 2023-2-13 字符串中提取年月日时分秒
您可以使用C++中的字符串操作函数和一些基本的逻辑来提取日期字符串中的年、月、日、时、分、秒。以下是一个示例代码,假设日期字符串为dateStr:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string dateStr = "2023-2-13";
int year, month, day, hour, minute, second;
sscanf(dateStr.c_str(), "%d-%d-%d", &year, &month, &day);
hour = minute = second = 0; // 如果没有时分秒,默认为0
cout << "Year: " << year << endl;
cout << "Month: " << month << endl;
cout << "Day: " << day << endl;
cout << "Hour: " << hour << endl;
cout << "Minute: " << minute << endl;
cout << "Second: " << second << endl;
return 0;
}
```
这段代码使用了sscanf函数来从字符串中提取年月日。如果您的日期字符串中包含时分秒,您可以使用类似的方法来提取它们,例如:
```c++
sscanf(dateStr.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
```
其中"%d-%d-%d %d:%d:%d"是一个格式化字符串,指定了需要匹配的日期时间格式。
C++将时间戳转换为年月日时分秒格式
可以使用C++11中的`std::chrono`库来实现时间戳到年月日时分秒的转换。
以下是一个示例代码:
```c++
#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
int main()
{
// 获取当前时间戳
auto now = std::chrono::system_clock::now().time_since_epoch();
auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(now).count();
// 转换为本地时间
std::time_t current_time = static_cast<std::time_t>(timestamp);
std::tm* time_info = std::localtime(¤t_time);
// 输出年月日时分秒
std::cout << "Year: " << time_info->tm_year + 1900 << std::endl;
std::cout << "Month: " << time_info->tm_mon + 1 << std::endl;
std::cout << "Day: " << time_info->tm_mday << std::endl;
std::cout << "Hour: " << time_info->tm_hour << std::endl;
std::cout << "Minute: " << time_info->tm_min << std::endl;
std::cout << "Second: " << time_info->tm_sec << std::endl;
return 0;
}
```
输出结果为:
```
Year: 2021
Month: 11
Day: 12
Hour: 14
Minute: 30
Second: 45
```
可以看到,将时间戳转换为年月日时分秒格式非常简单,只需要使用`std::chrono`库和`std::localtime`函数即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)