How to get the AM or PM value from a DateTime object in C# ?


This content was proof-read by Grammarly. You can Try Grammary for FREE

There are times when you might want to get only the string AM or PM from the DateTime object that you have.

How to get the AM or PM value from a DateTime object in C# ?

You can get this by using the format specifiers in the ToString method as shown in the code snippet.

using System;

namespace AbundantcodeApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var Data = 
                DateTime.Now.ToString("tt", System.Globalization.CultureInfo.InvariantCulture);
            
            Console.WriteLine(Data);
            Console.ReadLine();
        }
    }
}
%d bloggers like this: