【问题标题】:how can you format just the time of a DateTime object in C#如何在 C# 中仅格式化 DateTime 对象的时间
【发布时间】:2011-10-24 01:33:00
【问题描述】:

我有一个 DateTime 对象。如何按以下格式仅打印时间:

 5:50 PM

 3:30 AM

【问题讨论】:

    标签: c# datetime time


    【解决方案1】:
    yourDateTimeVariable.ToString("h:mm tt");
    

    【讨论】:

      【解决方案2】:
          DateTime dt = DateTime.Now;
          Console.WriteLine(dt.ToString("h:mm tt", new System.Globalization.CultureInfo("en-us")));
      

      试试这个

      【讨论】:

        【解决方案3】:

        尝试对您的日期对象执行 .ToShortTimeString()。例如:

        DateTime.Now.ToShortTimeString();
        

        【讨论】:

          猜你喜欢
          • 2011-09-01
          • 2020-02-26
          • 2017-07-25
          • 2023-03-16
          • 2011-08-14
          • 2020-09-04
          • 1970-01-01
          相关资源
          最近更新 更多