Date and Time as a string

This is what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <ctime>
#include <string>
using namespace std;
void date_and_time(string &date, string &time){
     time_t current_time;
     char date_hold[8];
     current_time = time(NULL);
     
     strftime(date_hold,sizeof(date_hold), "%Y%m%d", localtime(&current_time));
     date = string(date_hold);
     
     strftime(date_hold,sizeof(date_hold), "%H%M%S", localtime(&current_time));
     time = string(date_hold);
}

Is there a way to make sure that the strings would not contain trailing spaces?
Why would they? Your formats specify a digit in the last position.

Note however that date_hold is not large enough. max_size must include room for the trailing null character. %Y%m%d is 8 characters (4 digit year), therefore, date_hold must be at least 9 characters.
Topic archived. No new replies allowed.