Tuesday, February 2, 2010

DateTime.ToString() display “A.M.” or “P.M.”


Is there any way to use the DateTime.ToString() method to display the meridiem of the time portion as "A.M." instead of "AM"?


here is the work around.

using System;
using System.Globalization;

class Program {
public static void Main() {
CultureInfo c = (CultureInfo)CultureInfo.CurrentCulture.Clone();
c.DateTimeFormat.AMDesignator = "A.M.";
c.DateTimeFormat.PMDesignator = "P.M.";
Console.WriteLine(DateTime.Now.ToString("tt",c));
}
}

This are a second way to try with a designator. But here you have a static date format and therefore miss the culture.


DateTime time = DateTime.Now;
string s = time.ToString("yyyy.MM.dd hh:mm:ss t.\\M.");
Console.WriteLine(s);



Cheers








No comments: