当前位置: 首页>>代码示例>>C#>>正文


C# DateTime.ToShortTimeString方法代码示例

本文整理汇总了C#中System.DateTime.ToShortTimeString方法的典型用法代码示例。如果您正苦于以下问题:C# DateTime.ToShortTimeString方法的具体用法?C# DateTime.ToShortTimeString怎么用?C# DateTime.ToShortTimeString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.DateTime的用法示例。


在下文中一共展示了DateTime.ToShortTimeString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
        // Initialize a DateTime object.
        Console.WriteLine("Initialize the DateTime object to May 16, 2001 3:02:15 AM.\n");
        DateTime dateAndTime = new System.DateTime(2001, 5, 16, 3, 2, 15);

        // Display the name of the current culture.
        Console.WriteLine($"Current culture: \"{CultureInfo.CurrentCulture.Name}\"\n");
        var dtfi = CultureInfo.CurrentCulture.DateTimeFormat;

        // Display the long date pattern and string.
        Console.WriteLine($"Long date pattern: \"{dtfi.LongDatePattern}\"");
        Console.WriteLine($"Long date string:  \"{dateAndTime.ToLongDateString()}\"\n");

        // Display the long time pattern and string.
        Console.WriteLine($"Long time pattern: \"{dtfi.LongTimePattern}\"");
        Console.WriteLine($"Long time string:  \"{dateAndTime.ToLongTimeString()}\"\n");

        // Display the short date pattern and string.
        Console.WriteLine($"Short date pattern: \"{dtfi.ShortDatePattern}\"");
        Console.WriteLine($"Short date string:  \"{dateAndTime.ToShortDateString()}\"\n");

        // Display the short time pattern and string.
        Console.WriteLine($"Short time pattern: \"{dtfi.ShortTimePattern}\"");
        Console.WriteLine($"Short time string:  \"{dateAndTime.ToShortTimeString()}\"\n");
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:33,代码来源:DateTime.ToShortTimeString

输出:

Current culture: "en-US"

Long date pattern: "dddd, MMMM d, yyyy"
Long date string:  "Wednesday, May 16, 2001"

Long time pattern: "h:mm:ss tt"
Long time string:  "3:02:15 AM"

Short date pattern: "M/d/yyyy"
Short date string:  "5/16/2001"

Short time pattern: "h:mm tt"
Short time string:  "3:02 AM"

示例2: DateTime.ToShortTimeString()

//引入命名空间
using System;

class MainClass
{
  public static void Main()
  {
    DateTime myDateTime11 = new DateTime(2004, 1, 15, 23, 2, 5);
    Console.WriteLine("myDateTime11 = " + myDateTime11);
    Console.WriteLine("myDateTime11.ToLongTimeString() = " +  myDateTime11.ToLongTimeString());
    Console.WriteLine("myDateTime11.ToShortTimeString() = " +  myDateTime11.ToShortTimeString());
  }
}
开发者ID:C#程序员,项目名称:System,代码行数:13,代码来源:DateTime.ToShortTimeString


注:本文中的System.DateTime.ToShortTimeString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。