- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Date and Time
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 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++ 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++ 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:






« Previous Tutorial Next Tutorial »