.net string formatting

Good day,

Just wondering something quick…

DateTime.Now.ToString("h:mm tt")

What is the proper way to… escape if that is the proper term, that html entity so that it will produce the output

8:53 am

and the non breaking space is rendered correctly be the browser?


“s” returns seconds in a DateTime ToString() conversion so try this:

DateTime.Now.ToString("h:mm tt").Replace(" ", " ")

Even better would be to set the CSS white-space property for that bit of output:

timeNow.Text = DateTime.Now.ToString("h:mm tt");
<span class="time"><asp:Literal id="timeNow" runat="server" /></span>
span.time { white-space:nowrap; }