String 24 Hour Foramate DateTime csharp
using System;
class Solution {
static void Main(String[] args) {
var time = Console.ReadLine().Trim();
MilitaryTime mt = new MilitaryTime(time);
Console.WriteLine(mt);
}
class MilitaryTime{
string newTime = null;
string originalTime = null;
string correctHour(string time){
var ampmLen = 2;
var ampm = time.Substring(time.Length-ampmLen, ampmLen);
var hourIndex = 0;
var hour = time.Split(':')[hourIndex];
var h = hour;
if(ampm.Equals("PM")){
h = (int.Parse(hour) + 12).ToString();
}
if(hour.Equals("12") || hour.Equals("24")){
if(ampm.Equals("AM")){
h = "00";
} else if(ampm.Equals("PM")){
h = "12";
}
}
return h;
}
string minutesAndSeconds(string time){
var ampmLen = 2;
var hourLen = 2;
var startIndex = hourLen; // since we are taking after hour HH:MM:SS
return time.Substring(startIndex, time.Length - ampmLen - hourLen);
}
public MilitaryTime(string time){
originalTime = time;
}
public override string ToString(){
if(string.IsNullOrEmpty(newTime)){
newTime = correctHour(originalTime) + minutesAndSeconds(originalTime);
}
return newTime;
}
}
}