【问题标题】:convert date format Www Mmm dd hh:mm:ss yyyy to dd hh:mm:ss string in c++将日期格式 Www Mmm dd hh:mm:ss yyyy 转换为 c++ 中的 dd hh:mm:ss 字符串
【发布时间】:2015-12-04 08:48:36
【问题描述】:

我想将日期格式“Www Mmm dd hh:mm:ss yyyy”转换为 c++ 中的字符串。
到目前为止,我一直在尝试:

char str[255]; 
char str1[255];
int y = 0;
int i;
time_t timer;
timer = GetTickCount();
strcpy(str, ctime(&timer));
sendBuff[strlen(str) - 1] = '\0'; //to remove the new-line from the created string

//to copy from the string the exact Time Without Date from the struct of Www Mmm dd hh:mm:ss yyyy - will take only hh:mm:ss
for (i = 11; i < 19; i++, y++)
        strcpy(&str1[y], &str[i]);

【问题讨论】:

  • 使用std::stringsubstr?
  • 或者为什么不使用std::strftime 从一开始就得到正确的字符串?
  • 嗨 Joachim Pileborg,谢谢你的回答,但我从来没有使用过这种类型的变量,你能做点更简单的吗? :)

标签: c++ string date datetime gettickcount


【解决方案1】:

Joachim 使用 substr 建议的代码如下所示

std::string temp = str.substr(11, 9); //from 11the place copy next 9 characters
strcpy(str1, temp.c_str());

【讨论】:

  • 谢谢!! :) 一点修正:std::string temp = str,substr(11, 9); //从11处复制下9个字符 strcpy(str1, temp.c_str());
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 2021-01-29
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
相关资源
最近更新 更多