top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How to print current date and time in C/C++ ?

+3 votes
69 views

How to print current date and time in C/C++, sample program will help?

posted Jan 11, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
#include<iostream>
#include<cmath>
#include <ctime>
using namespace std;

int main()
{

        time_t t = time(NULL);
 tm* timePtr = localtime(&t);

 cout << "seconds= " << (timePtr->tm_sec) << endl;
 cout << "minutes = " << (timePtr->tm_min) << endl;
 cout << "hours = " << (timePtr->tm_hour) << endl;
 cout << "day of month = " << (timePtr->tm_mday) << endl;
 cout << "month of year = " << (timePtr->tm_mon)+1 << endl;
 cout << "year = " << (timePtr->tm_year)+1900 << endl;
 cout << "weekday = " << (timePtr->tm_wday )<< endl;
 cout << "day of year = " << (timePtr->tm_yday )<< endl;
 cout << "daylight savings = " <<(timePtr->tm_isdst )<< endl;
        cout << endl;
        cout << endl;


        cout << "Date     " <<(timePtr->tm_mday)<<"/"<< (timePtr->tm_mon)+1 <<"/"<< (timePtr->tm_year)+1900<< endl;
        cout << "Time     " << (timePtr->tm_hour)<<":"<< (timePtr->tm_min)<<":"<< (timePtr->tm_sec) << endl; 
    return 0;
}

OUTPUT:seconds= 41minutes = 53hours = 16day of month = 17month of year = 5year = 2013weekday = 5day of year = 136daylight savings = 1
Date 17/5/2013Time 16:53:41

answer Jan 11, 2016 by Shivaranjini
Similar Questions
+2 votes

I always hate dealing with date/time stuff in php - never get it even close until an hour or two goes by....

anyway I have this:

// get two timestamp values
$exp_time = $_COOKIE[$applid."expire"];
$curr_time = time();
// get the difference
$diff = $exp_time - $curr_time;
// produce a display time of the diff
$time_left = date("h:i:s",$diff);

Currently the results are:

exp_time is 06:55:07
curr_time is 06:12:03
the diff is 2584
All of these are correct.

BUT time_left is 07:43:04 when it should be only "00:43:04". So - where is the hour value of '07' coming from?? And how do I get this right?

+2 votes

I have a date field on an html form that users may leave blank. If they do leave it blank I want to write the date 01/01/1901 into the mysql table. How can I accomplish this and where in my .php script file should I put the code?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...