C++ program to change the time format from 12 hours to 24 hours
In this tutorial, we will learn how to change the time format from 12 hours to 24 hours in C++ programming. In this program, the user needs to enter the input in the 12 hours format and the output will be in 24 hours format.
So, let us see an example for better understanding
input: 08 : 05 : 25 PM
output: 20 : 05 : 25 PM
As told the inputs from the user will be in 12 hours format whereas the output will be in 24 hours format.
Things we need to know before starting the program
- Midnight 12 : 00 : 00 AM in 12 hours clock isĀ 00 : 00 : 00 in 24 hours clock.
- Afternoon 12 : 00 : 00 PM in 12 hours clock is 12 : 00 : 00 in 24 hours clock.
How to change the time format from 12 hours to 24 hours
There are two-time medians such as AM (Ante Meridiem) and PM (Post Meridiem).
The time will always be in the format of hours : minutes : seconds ( hh : mm : ss)
Now let us see the program on how to change the time format from 12 hours to 24 hours.
In the below program we use few functions such as setfill(), setw(), setcmp(). Let us see the detailed explanation on them below.
setfill(): setfill function is used to fill the character where the character is specified as parameter.
syntax: setfill(char c)
setw(): setw function shows the number of characters that is to be displayed before any particular field.
syntax: setw(int n)
strcmp(): strcmp function is the built-in library function where we can compare the two strings.
For a better understanding of the below program kindly refer to the below links
Convert 24 hour format to 12 hour format in C++
Program:
#include<iostream> #include<string.h> #include <iomanip> using namespace std; int main() { int hour,min,sec,hour1,min1,sec1; char median[10]; cout<<"Enter the time in hours, minutes and seconds format:"; cin>>hour; cin>>min; cin>>sec; cout<<"Enter the time median AM/PM:"; cin>>median; cout<<"12 hours time format:"<<setfill('0') << setw(2) <<hour<<":"<<setfill('0') << setw(2) <<min<<":"<<setfill('0') << setw(2) <<sec<<median<<endl; if(strcmp(median,"PM")==0) { if (hour<12) { hour1=hour+12; min1=min; sec1=sec; cout<<"24 hours time format:" <<hour1<<":"<<setfill('0') << setw(2) <<min1<<":"<<setfill('0') << setw(2) <<sec1<<median; } else if(hour=12) { hour1=12; min1=min; sec1=sec; cout<<"24 hours time format:"<<hour1<<":"<<setfill('0') << setw(2) <<min1<<":"<<setfill('0') << setw(2) <<sec1<<median; } } else if(strcmp(median,"AM")==0) { if (hour<12) { hour1=hour; min1=min; sec1=sec; cout<<"24 hours time format:"<<setfill('0') << setw(2) <<hour1<<":"<<setfill('0') << setw(2) <<min1<<":"<<setfill('0') << setw(2) <<sec1<<median; } else if(hour=12) { min1=min; sec1=sec; cout<<"24 hours time format:"<<"00"<<":"<<setfill('0') << setw(2) <<min1<<":"<<setfill('0') << setw(2) <<sec1<<median; } } else { printf("The given time is wrong"); } }
Output:
Enter the time in hours, minutes and seconds format:05 45 55 Enter the time median AM/PM:PM 12 hours time format:05:45:55PM 24 hours time format:17:45:55PM Enter the time in hours, minutes and seconds format:02 45 34 Enter the time median AM/PM:AM 12 hours time format:02:45:34AM 24 hours time format:02:45:34AM
Leave a Reply