简体   繁体   中英

Convert string time like (0740) into 12 hours (AM,PM) time format like (07:40 AM)

How can i convert string time into 12 hours (AM,PM) time format? Like if input string will like "2320" than i want answer like "11:20 PM".

string s = DateTime.ParseExact("2320","HHmm",CultureInfo.CurrentCulture)
       .ToString("hh:mm tt");

I'm sure its been mentioned many times, but here is the reference for the to string parameters for Datetime here .

In Marc's answer, there is a leading zero in the hours if it is a single digit hour.

string s = DateTime.ParseExact("2320", "HHmm").ToString("h:mm tt");

The above would provide the format we're used to seeing from most digital clocks these days.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM