Hour from DateTime? in 24 hours format

前端 未结 5 744
迷失自我
迷失自我 2020-12-01 03:57

So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.
For example:
If the hour is 2:20:23 p.m. i want to convert it to

5条回答
  •  醉梦人生
    2020-12-01 04:45

    Try this, if your input is string 
    For example 
    
    string input= "13:01";
    string[] arry = input.Split(':');                  
    string timeinput = arry[0] + arry[1];
    private string Convert24To12HourInEnglish(string timeinput)
    
     {
    
    DateTime startTime = new DateTime(2018, 1, 1, int.Parse(timeinput.Substring(0, 2)), 
    int.Parse(timeinput.Substring(2, 2)), 0); 
    return startTime.ToString("hh:mm tt");
    
    }
    out put: 01:01
    

提交回复
热议问题