DateTime.ToString () displays the meridiem as "AM" or "PM"

Is it possible to use the DateTime.ToString () method to display the meridium of the time part as "AM" instead of "AM"? I tried using the mask "tt" but it just displays "AA"

+5
source share
2 answers

You can override the properties AMDesignatorand PMDesignator CultureInfo.DateTimeFormat, and then specify the culture as the format provider:

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));
    }
}
+14
source

Use "t.\\M":

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

Output:

2010.02.02 09:26:14 A.M.

: t AM/PM ( DateTimeFormatInfo.AMDesignator DateTimeFormatInfo.PMDesignator). \\M M, DateTime.ToString .

, , . , AM/PM , .

+12

All Articles