# convert current time into seconds?

Page 1 of 1

## 6 Replies - 31873 Views - Last Post: 07 May 2008 - 03:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=50727&amp;s=8d3385c5510a1aa442ad06736a66ed47&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chronoTrigger

Reputation: 3
• Posts: 60
• Joined: 08-October 07

# convert current time into seconds?

Posted 01 May 2008 - 10:45 AM

does anyone know how can I get the total seconds based on the current time?

hour:minute into seconds
Is This A Good Question/Topic? 0

## Replies To: convert current time into seconds?

### #2 Martyr2

• Programming Theoretician

Reputation: 5207
• Posts: 13,953
• Joined: 18-April 07

## Re: convert current time into seconds?

Posted 01 May 2008 - 10:51 AM

(hour * 3600) + (minutes * 60) = total number of seconds

### #3 lockdown

• D.I.C Regular

Reputation: 6
• Posts: 394
• Joined: 29-September 07

## Re: convert current time into seconds?

Posted 01 May 2008 - 10:54 AM

Ah beat me to it. Yeah thats what I would recommend.

This post has been edited by lockdown: 01 May 2008 - 10:54 AM

### #4 chronoTrigger

Reputation: 3
• Posts: 60
• Joined: 08-October 07

## Re: convert current time into seconds?

Posted 01 May 2008 - 10:55 AM

Martyr2, on 1 May, 2008 - 10:51 AM, said:

(hour * 3600) + (minutes * 60) = total number of seconds

thanks for the calculation method, but I meant, the C++ function itself that can access to current time and hopefully can access to both hour and minute so I can calculate into seconds like you have shown above. Sorry my question is kind of vague.

This post has been edited by chronoTrigger: 01 May 2008 - 10:56 AM

### #5 Martyr2

• Programming Theoretician

Reputation: 5207
• Posts: 13,953
• Joined: 18-April 07

## Re: convert current time into seconds?

Posted 01 May 2008 - 11:26 AM

I knew it couldn't be that simple. Next time work on the vagueness a little. Precise questions deserve precise answers I always say.

```#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;

int main ()
{
// Create a raw time_t variable and a tm structure
time_t rawtime;
struct tm * timeinfo;

// Get the current time and place it in time_t
time ( &rawtime );

// Get the locatime from the time_t and put it into our structure timeinfo
timeinfo = localtime ( &rawtime );

// Now we have access to hours, minutes, seconds etc as member variables of all type int
int hour = timeinfo->tm_hour;
int min = timeinfo->tm_min;

// Just print out the hours and minutes to show you
cout << "Hour is: " << hour << " and minutes are: " << min << endl;

return 0;
}

```

Just an example for you to play with. Enjoy!

"At DIC we be time mastering code ninjas... with that said, I say it is miller time!"

### #6 chronoTrigger

Reputation: 3
• Posts: 60
• Joined: 08-October 07

## Re: convert current time into seconds?

Posted 01 May 2008 - 11:30 AM

Martyr2, on 1 May, 2008 - 11:26 AM, said:

I knew it couldn't be that simple. Next time work on the vagueness a little. Precise questions deserve precise answers I always say.

```#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;

int main ()
{
// Create a raw time_t variable and a tm structure
time_t rawtime;
struct tm * timeinfo;

// Get the current time and place it in time_t
time ( &rawtime );

// Get the locatime from the time_t and put it into our structure timeinfo
timeinfo = localtime ( &rawtime );

// Now we have access to hours, minutes, seconds etc as member variables of all type int
int hour = timeinfo->tm_hour;
int min = timeinfo->tm_min;

// Just print out the hours and minutes to show you
cout << "Hour is: " << hour << " and minutes are: " << min << endl;

return 0;
}

```

Just an example for you to play with. Enjoy!

"At DIC we be time mastering code ninjas... with that said, I say it is miller time!"

Thanks I will definitely play around with your given example.

### #7 chronoTrigger

Reputation: 3
• Posts: 60
• Joined: 08-October 07

## Re: convert current time into seconds?

Posted 07 May 2008 - 03:31 PM

I don't know why, but I included the header file like your sample code shown above, my program has error, so I'm using SYSTEMTIME instead, and I believe I can access to hour, minute, second. So far it works.

Thanks Martyr2.