Time Conversion

  • + 0 comments

    C# without using DateTime:

    public static string timeConversion(string s)
    {
        bool isPM = s.Substring(8, 2) == "PM";
        string hourFormat = s.Substring(0, 8);
        int hour = int.Parse(hourFormat.Substring(0, 2));
        hourFormat = hourFormat.Remove(0, 2);
    
        if (!isPM)
        {
            if(hour == 12)
            return "00" + hourFormat;
    
            return hour < 9 ? "0" + hour + hourFormat : hour + hourFormat;
        }
    
        hour = hour == 12 ? hour : hour + 12;
        
    
        return hour + hourFormat;
    }