इस ट्यूटोरियल में, हम 24 घंटे की घड़ी से 12 घंटे की घड़ी फॉर्मेट में समय बदलने के प्रोग्राम पर चर्चा करेंगे।
इसके लिए हमें 24 घंटे के फॉर्मेट में निश्चित समय दिया जाएगा। हमारा कार्य इसे "AM" या "PM" एक्सटेंशन के साथ 12 घंटे के फॉर्मेट में बदलना है।
उदाहरण
#include <bits/stdc++.h>
using namespace std;
// 12 घंटे फॉर्मेट में बदलना
void convert_12hour(string str) {
int h1 = (int)str[0] - '0';
int h2 = (int)str[1] - '0';
int hh = h1 * 10 + h2;
// एक्सटेंशन ढूंढना
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;
}
आउटपुट
5:35:20 PM