Marc Roussel Marc Roussel - 1 year ago 87
C# Question

tt not shown when formatting DateTime in C#

With this specific code I don't have the AM PM shown. I tried searching Stack Overflow but did not find anything regarding this specific format. Is there something wrong in the code that prevents the tt to be shown ?

DateTime.Now.ToString("dddd d MMMM yyyy hh:mm tt", new System.Globalization.CultureInfo(SessionIsFrench.Value ? "fr-CA" : "en-US"));

The SessionIsFrench is true

Answer Source

If you really need the AM/PM part, you could format the date and time parts separately, and always use "en-US" for the time.

var now = DateTime.Now;
var date = now.ToString("dddd d MMMM", new System.Globalization.CultureInfo(SessionIsFrench.Value ? "fr-CA" : "en-US"));
var time = now.ToString("hh:mm tt", new System.Globalization.CultureInfo("en-US"));
var dateTime = string.Format("{0} {1}", date, time);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download