get current time using time(0)

I have this project to get the current time and date using the time(0) function which resets to the time to 1970/1/1 00:00:00.

to get current time, i would have to use math in order to get the current time and date. below one is the one to get the current year, 2012.

ive been trying for a while but how can i get the month / day ( leap year included) / hour / min / sec?

thanks for the help

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>
#include <time.h>


using namespace std;


int main()
{
int lol;
int totaltimes;
time (0);
cout << time (0)<<endl;
totaltimes = time(0);
cout<<"totaltimes="<<totaltimes<<endl;
double divisor = (60 * 60 * 24 * 365);
cout<<"cur year="<<int((totaltimes/divisor)+1970)<<endl;
cin>>lol;
}
If you want to use time(0) to get the date, you'll have to write a if statement for # years, then determine if current year is leap year, days remaining, etc...

If you just want the output and the project doesn't have to use time(0) you might look at http://www.cplusplus.com/reference/ctime/time/.

to use time(0)

The year is easy right ?

The month and day are a bit harder
determine if this year is leap year
if yes, determine #days left in year
if #days left in year > 60 then the current date is after Feb 29
if no the current date is before Feb 29
once you know that, there are lots of ways to get the day of month.
a simple if statement would work or you can get more fancy

if (#days left in year < 31) Jan
else if leapyear=current;
if (#days left in year < 60) Feb
else
if (#days left in year > 60) March-Dec

Or
if (#days left in year > 31) not Jan
Subtract 31 from days left in year
if leapyear=current;
if (#days left in year > 29) not Feb
subtract 29 from days left


Once you get the month, subtract the number of days in previous months and you have the day, or use the 2nd method which gives you the days left for the current month.

Played around with this some and came up with this.
The hours, mins and seconds are simply the remainder.
You'll have to change CSTTimeZone to what ever your timezone change from GMT is.

All that is left is the month and day.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>
#include <time.h>
#include <math.h>

using namespace std;
int CSTTimeZone=6;
int main()
{
cout << "Times below are from 1/1/1970"<< endl;
time (0);
cout << "Seconds = " << time (0)<<endl;
cout << "Minutes = " << (time (0)/60)<<endl;
cout << "Hours   = " << (time (0)/60/60)<<endl;
cout << "Days    = " << (time (0)/60/60/24)<<endl;
cout << "Years   = " << (time (0)/60/60/24/365)<<endl;

// Determine if Leap Year
float LeapYearChk=(((time (0)/60/60/24/365)-2) % 4);

if (LeapYearChk==0)
{
     cout << "This is Leap year" << endl;
     if (((time(0)/60/60/24)-(((time(0)/60/60/24/365)*365)))>60)
     {
     cout << "Leapyear already occured this year"<< endl;
     cout << "There have been ";
     cout << ((((time (0)/60/60/24/365)+1970)-1972)/4)+1;
     cout << " Leap Years"  << endl;
     }
     else
     {
     cout << "Leapyear has not occured this year"<< endl;
     cout << "There have been ";
     cout << ((((time (0)/60/60/24/365)+1970)-1972)/4);
     cout << " Leap Years" << endl;
     }
}
cout << "------------------------"<< endl;
cout << "Current Year = " << ((time (0)/60/60/24/365)+1970) << endl;
cout << "Julian Date  = " <<((time(0)/60/60/24)-(((time(0)/60/60/24/365)*365))) << endl;
cout << "Current time = ";
cout << ((time (0)/60/60)-CSTTimeZone) %24  << ":" ;  // hours
cout << (time (0)/60) %60 << ":" ;  // minutes
cout << (time (0)) %60 << endl;  // seconds
return 0;
}


Ps. I know there are easier methods, This is just one I came up with looking at your code.

If you want to see something really fancy, go to http://unicorn.us.com/cal.html and look at their calendar code. It's awesome.
Last edited on
Topic archived. No new replies allowed.