Search code examples
c++stringdatedatetimegettickcount

convert date format Www Mmm dd hh:mm:ss yyyy to dd hh:mm:ss string in c++


i want to convert date format "Www Mmm dd hh:mm:ss yyyy", and copy just "dd hh:mm:ss" to a string in c++.
i have been tring so far it came like:

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]);

Solution

  • Code will look like below as suggested by Joachim using substr

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