Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2042
  • Last Modified:

How to get in C++ this time format HH.MM.SS?

Hello Experts,
how can i get this time format?
for ex. 17.22.34

i need a string as result
0
kagami01
Asked:
kagami01
  • 4
  • 2
1 Solution
 
Infinity08Commented:
You can use strftime :

        http://www.cplusplus.com/reference/clibrary/ctime/strftime.html
time_t now = time(0);
char timestamp[10] = "";
strftime (timestamp, 10,"%H.%M.%S", localtime(&now));

Open in new window

0
 
waysideCommented:
#include <sys/types.h>
#include <sys/timeb.h>

struct tm *current_time;

current_time = localtime(time());

char timebuf[20] = { 0 } ;

strftime(timebuf, 20, "%H.%M.%S", current_time);

0
 
kagami01Author Commented:
i can use even the results of strftime even with asctime??
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Infinity08Commented:
>> i can use even the results of strftime even with asctime??

asctime is another (fixed) way of formatting a time into a string. strftime allows you to choose the format, so you'll need strftime as I showed.
0
 
kagami01Author Commented:
so how to convert the output of strftime in a string??
0
 
Infinity08Commented:
>> so how to convert the output of strftime in a string??

strftime already places its output in a string. In my example, the string is called timestamp. You can output it by adding this for example :
time_t now = time(0);
char timestamp[10] = "";
strftime (timestamp, 10,"%H.%M.%S", localtime(&now));
 
std::cout << timestamp << std::endl;                     // <--- this

Open in new window

0
 
Infinity08Commented:
Or if you mean you want it as an std::string, you can always create one from the timestamp buffer :
time_t now = time(0);
char timestamp_buf[10] = "";
strftime (timestamp_buf, 10,"%H.%M.%S", localtime(&now));
 
std::string timestamp = std::string(timestamp_buf);
std::cout << timestamp << std::endl;

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now