Marc Roussel Marc Roussel - 3 months ago 11
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

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);
Comments