codescracker
c++

C++ Date and Time



« Previous Tutorial Next Tutorial »

The C++ standard library doesn't provide any proper date type. C++ inherits the structs and functions for date and time manipulation from the C language. To access date and time related functions and structures, you have to use/include a header file in your C++ program named time.h.

There are four time-related types: clock_t, time_t, size_t, and tm. The types clock_t, size_t and time_t are capable of representing the system time and date as some sort of integer.

The structure type tm holds the date and time in the form of a C structure having the elements given here in these statements:

struct tm
{
   int tm_sec;      // seconds of minutes from 0 to 61
   int tm_min;      // minutes of hour from 0 to 59
   int tm_hour;     // hours of day from 0 to 24
   int tm_mday;     // day of month from 1 to 31
   int tm_mon;      // month of year from 0 to 11
   int tm_year;     // year since 1900
   int tm_wday;     // days since Sunday
   int tm_yday;     // days since January 1st
   int tm_isdst;    // hours of daylight savings time
}

The tm structure is very important when working with the date and time in either C or C++ language. This structure holds the date and time in the form of a C structure. Most of the time related functions makes use of tm structure. Let's take some examples on date and time in C++ for complete understanding on C++ date and time.

C++ Date and Time Example

This C++ program simply prints the local and UTC date and time on the screen.

/* C+ Date and Time - Example Program of C++ Date and Time
 * This program prints local date and time, UTC date and
 * time on the output screen */

#include<iostream.h>
#include<conio.h>
#include<time.h>

void main()
{
   clrscr();

   time_t now = time(0);   // current system date/time

   char* dt = ctime(&now);   // convert in string form

   cout<<"Local date and time is:-\n\t\t"<<dt<<"\n";

   tm *gmtm = gmtime(&now);   // converting now to tm struct for UTC date/time
   dt = asctime(gmtm);
   cout<<"UTC date and time is:-\n\t\t"<<dt;

   getch();
}

Here is the sample output of the above C++ program:

c++ date time

C++ Date Example

This C++ program prints the current or today's date on the output screen.

/* C++ Date and Time - Example Program of C++ Date and Time
 * This program prints today's date on the output screen */

#include<iostream.h>
#include<conio.h>
#include<time.h>

void main()
{
   clrscr();

   time_t now = time(0);
   tm *ltm = localtime(&now);
   cout<<"Today is ";
   cout<<ltm->tm_mday<<"/"<<1+ltm->tm_mon<<"/";
   cout<<1900+ltm->tm_year<<"\n";

   getch();
}

Here is the sample output of this C++ program:

c++ date

C++ Time Example

This C++ program prints the current time on the output screen.

/* C++ Date and Time - Example of C++ Date and Time
 * This program prints current date on the output
 * output screen in C++ */

#include<iostream.h>
#include<conio.h>
#include<time.h>

void main()
{
   clrscr();

   time_t now = time(0);
   tm *ltm = localtime(&now);
   cout<<"The current time is ";
   cout<<1+ltm->tm_hour<<":";
   cout<<1+ltm->tm_min<<":";
   cout<<1+ltm->tm_sec<<"\n";

   getch();
}

Below is the sample run of the above C++ program:

c++ time

C++ Date and Time Complete Program

This is the complete version of the date and time programs (as shown above) in C++.

/* C++ Date and Time - Example of C++ Date and Time
 * This program demonstrates the working of date
 * and time in C++ program */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void main()
{
   clrscr();
   char choice, ans;

   time_t now = time(0);
   tm *ltm = localtime(&now);

   do
   {
      cout<<"1. Time\n";
      cout<<"2. Date\n";
      cout<<"3. Month\n";
      cout<<"4. Year\n";
      cout<<"5. All\n";
      cout<<"6. Exit\n";
      cout<<"\nEnter your choice (1-6) ";
      cin>>choice;
      clrscr();
      switch(choice)
      {
         case '1':
             cout<<"The current time is ";
             cout<<1+ltm->tm_hour<<":";
             cout<<1+ltm->tm_min<<":";
             cout<<1+ltm->tm_sec<<"\n";
             break;
         case '2':
            cout<<"Today's date is ";
            cout<<ltm->tm_mday<<"\n";
            break;
         case '3':
            cout<<"It's ";
            cout<<1+ltm->tm_mon<<"th month\n";
            break;
         case '4':
            cout<<"This is ";
            cout<<1900+ltm->tm_year<<"\n";
            break;
         case '5':
            cout<<"Time: "<<1+ltm->tm_hour<<":";
            cout<<1+ltm->tm_min<<":";
            cout<<1+ltm->tm_sec<<"\n";
            cout<<"Date: "<<ltm->tm_mday<<"\n";
            cout<<"Month: "<<1+ltm->tm_mon<<"\n";
            cout<<"Year: "<<1900+ltm->tm_year<<"\n";
            break;
         case '6':
            exit(1);
         default:
            cout<<"Wrong choice..!!..Press a key to exit..\n";
            getch();
            exit(2);
      }
      cout<<"\nWant to check more ? (y/n).. ";
      cin>>ans;
      cout<<"\n";
   }while(ans=='y' || ans=='Y');

   getch();
}

Here are some sample runs of the above C++ program:

c++ date time example

c++ date time program

date time c++

c++ date example

c++ time example

c++ print current date time

C++ Online Test


« Previous Tutorial Next Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker