Remove AM or PM from Time



We can remove am or pm from the time string in the following way

string FormatTime(string TimeWithAmPm)  
    {  
        string TimeWithoutAmPm = string.Empty;  
        string AmOrPm = TimeWithAmPm.Substring(TimeWithAmPm.Length - 2);  
        switch (AmOrPm)  
        {  
            case ("AM"):  
                TimeWithoutAmPm = TimeWithAmPm.Replace("AM", "");  
                break;  
            case ("PM"):  
                int hour = 0;  
                int.TryParse(TimeWithAmPm.Replace("PM", ""), out hour);  
                TimeWithoutAmPm = (hours + 12).ToString();  
                break;  
        }  
        return TimeWithoutAmPm;  
    }

0 Comment's

Comment Form