Time display format

I want my time to print only in hh:mm:ss format ... I don't want date and year

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include <ctime>

using namespace std;
  
int main()
{
    time_t tt = time(0);
  
    cout<<"time is -- > "<<ctime(&tt);

    return 0;
}


output is like this::
time is -- > Sun Jan 23 20:17:08 2022


I want it like this::
time is -- > 20:17:08



Please help me!!
Last edited on
Last edited on
I am getting so many errors. I guess my compiler is not up to date.

can you suggest something else @jonnin
Use strftime() if you want fine detailed control over the output format.
https://www.cplusplus.com/reference/ctime/strftime/
The format returned from ctime() is fixed (www mmm dd hh:mm:ss yyyy). So to just display the time then simply:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <ctime>
#include <string_view>

using namespace std;

int main() {
	const auto tt {time(0)};

	cout << "time is -- > " << std::string_view(ctime(&tt) + 11, 8) << '\n';
}


Note that ctime() overwrites the same internal buffer every time it is used.

Last edited on
Topic archived. No new replies allowed.