How do I get the AM/PM value from a DateTime?

SilverLight picture SilverLight · Oct 24, 2011 · Viewed 334.3k times · Source

The code in question is below:

public static string ChangePersianDate(DateTime dateTime)
{
    System.Globalization.GregorianCalendar PC = new System.Globalization.GregorianCalendar();
    PC.CalendarType = System.Globalization.GregorianCalendarTypes.USEnglish;
    return
    PC.GetYear(dateTime).ToString()
    + "/"
    + PC.GetMonth(dateTime).ToString()
    + "/"
    + PC.GetDayOfMonth(dateTime).ToString()
    + ""
    + PC.GetHour(dateTime).ToString()
    + ":"
    + PC.GetMinute(dateTime).ToString()
    + ":"
    + PC.GetSecond(dateTime).ToString()
    + " "
    ????????????????
}

how can I get the AM/PM from the dateTime value?

Answer

Andy picture Andy · Oct 24, 2011

How about:

dateTime.ToString("tt", CultureInfo.InvariantCulture);