

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert time from 24 hour clock to 12 hour clock format in C++
In this tutorial, we will be discussing a program to convert time from 24 hour clock to 12 hour clock format.
For this we will be provided with certain time in 24 hour format. Our task is to convert it into 12 hour format with the extension of “AM” or “PM”.
Example
#include <bits/stdc++.h> using namespace std; //converting into 12 hour format void convert_12hour(string str){ int h1 = (int)str[0] - '0'; int h2 = (int)str[1] - '0'; int hh = h1 * 10 + h2; //finding the extension string Meridien; if (hh < 12) { Meridien = "AM"; } else Meridien = "PM"; hh %= 12; if (hh == 0) { cout << "12"; for (int i = 2; i < 8; ++i) { cout << str[i]; } } else { cout << hh; for (int i = 2; i < 8; ++i) { cout << str[i]; } } cout << " " << Meridien << '\n'; } int main(){ string str = "17:35:20"; convert_12hour(str); return 0; }
Output
5:35:20 PM
- Related Questions & Answers
- C# program to convert time from 12 hour to 24 hour format
- Python program to convert time from 12 hour to 24 hour format
- C++ program to convert time from 12 hour to 24 hour format
- Converting 12 hour format time to 24 hour format in JavaScript
- How to convert 12-hour time scale to 24-hour time in R?
- Java Program to display time in 24-hour format
- 24-hour time in Python
- Java Program to display Time in 12-hour format
- How to convert string to 24-hour datetime format in MySQL?
- Format hour in k (1-24) format in Java
- Format hour in kk (01-24) format in Java
- Python Pandas - Format the Period object and display the Time with 24-Hour format
- Program to convert hour minutes’ time to text format in Python
- Program to find angle between hour and minute hands of a clock in C++?
- JavaScript program to convert 24 hours format to 12 hours
Advertisements