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

前端 未结 8 989
陌清茗
陌清茗 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:44

    Try This Code:

    int timezone = 0;
    

    This string gives 12-hours format

    string time = DateTime.Now.AddHours(-timezone).ToString("hh:mm:ss tt");
    

    This string gives 24-hours format

    string time = DateTime.Now.AddHours(-timezone).ToString("HH:mm:ss tt");
    

提交回复
热议问题