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

后端 未结 14 1578
刺人心
刺人心 2020-11-28 06:23

The code in question is below:

public static string ChangePersianDate(DateTime dateTime)
{
    System.Globalization.GregorianCalendar PC = new System.Globa         


        
相关标签:
14条回答
  • 2020-11-28 06:55

    You can test it in this way

    Console.WriteLine(DateTime.Now.ToString("tt "));
    

    The output will be like this:

    AM
    

    or

    PM
    
    0 讨论(0)
  • 2020-11-28 06:55

    If you want to add time to LongDateString Date, you can format it this way:

        DateTime date = DateTime.Now;
        string formattedDate = date.ToLongDateString(); 
        string formattedTime = date.ToShortTimeString();
        Label1.Text = "New Formatted Date: " + formattedDate + " " + formattedTime;
    

    Output:

    New Formatted Date: Monday, January 1, 1900 8:53 PM 
    
    0 讨论(0)
  • 2020-11-28 06:57

    Something like bool isPM = GetHour() > 11. But if you want to format a date to a string, you shouldn't need to do this yourself. Use the date formatting functions for that.

    0 讨论(0)
  • 2020-11-28 07:01

    its pretty simple

      Date someDate = new DateTime();
      string timeOfDay = someDate.ToString("hh:mm tt"); 
      // hh - shows hour and mm - shows minute - tt - shows AM or PM
    
    0 讨论(0)
  • 2020-11-28 07:04

    How about:

    dateTime.ToString("tt", CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2020-11-28 07:04
    string AM_PM = string.Format("{0:hh:mm:ss tt}", DateTime.Now).Split(new char[]{' '})[1];
    
    0 讨论(0)
提交回复
热议问题