2 Replies - 3860 Views - Last Post: 16 November 2010 - 03:32 AM Rate Topic: -----

#1 Guest_Hassan*


Reputation:

How to display system hour in c++

Posted 16 November 2010 - 02:58 AM

I have seen many examples for displaying time but I couldn't find any which was simple and easy to use. Anyone please guide me how to display system time in C++. I only want to display hours not the date nor time or seconds.
Is This A Good Question/Topic? 0

Replies To: How to display system hour in c++

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,383
  • Joined: 10-May 07

Re: How to display system hour in c++

Posted 16 November 2010 - 03:04 AM

Knowing the Operating System you are coding for would be helpful.
Was This Post Helpful? 0
  • +
  • -

#3 Sloot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 29-March 10

Re: How to display system hour in c++

Posted 16 November 2010 - 03:32 AM

First:
"ctime Convert time_t value to string (function)"

Second:

"char * ctime ( const time_t * timer );
Convert time_t value to string

Converts the time_t object pointed by timer to a C string containing a human-readable version of the corresponding local time and date.

The returned string has the following format:

Www Mmm dd hh:mm:ss yyyy
"

Thus:
#include <time.h>

time_t rawtime;
time(&rawtime);

//rawtime now holds the time in string formated as above. 
//since you want hours (pos 8 and 9), and strings can be used
//just like arrays:
char hours[2];
hours[0]=rawtime[8];
hours[1]=rawtime[9];

cout<<hours;


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1