how to convert 24-hour format TimeSpan to 12-hour format TimeSpan?

前端 未结 8 937
陌清茗
陌清茗 2021-01-04 05:17

I have TimeSpan data represented as 24-hour format, such as 14:00:00, I wanna convert it to 12-hour format, 2:00 PM, I googled and found something related in stackoverflow a

8条回答
  •  再見小時候
    2021-01-04 05:47

    It is very simple, Let's suppose we have an object ts of TimesSpan : TimeSpan ts = new TimeSpan(); and suppose it contains some value like 14:00:00 Now first convert this into a string and then in DateTime as following:

    TimeSpan ts = new TimeSpan(); // this is object of TimeSpan and Suppose it contains
                                  // value 14:00:00
    string tIme = ts.ToString(); // here we convert ts into String and Store in Temprary
                                 // String variable.
     DateTime TheTime = new DateTime(); // Creating the object of DateTime;
     TheTime = Convert.ToDateTime(tIme); // now converting our temporary string into DateTime;
     Console.WriteLine(TheTime.ToString(hh:mm:ss tt));
    

    this will show the Result as: 02:00:00 PM

提交回复
热议问题