C#의 DateTime.ToShortTimeString() 메서드는 현재 DateTime 개체의 값을 해당하는 짧은 시간 문자열 표현으로 변환하는 데 사용됩니다.
구문
다음은 구문입니다 -
public string ToShortTimeString ();
예시
이제 DateTime.ToShortTimeString() 메서드를 구현하는 예를 살펴보겠습니다. -
using System;
using System.Globalization;
public class Demo {
public static void Main() {
DateTime d = DateTime.Now;
Console.WriteLine("Date = {0}", d);
Console.WriteLine("Current culture = "+CultureInfo.CurrentCulture.Name);
var pattern = CultureInfo.CurrentCulture.DateTimeFormat;
string str = d.ToShortTimeString();
Console.WriteLine("Short time string = {0}", pattern.ShortTimePattern);
Console.WriteLine("Short time string representation = {0}", str);
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Date = 10/16/2019 8:59:23 AM Current culture = en-US Short time string = h:mm tt Short time string representation = 8:59 AM
예시
이제 DateTime.ToShortTimeString() 메서드를 구현하는 또 다른 예를 살펴보겠습니다.
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 11, 11, 7, 11, 25);
Console.WriteLine("Date = {0}", d);
string str = d.ToShortTimeString();
Console.WriteLine("Short time string representation = {0}", str);
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Date = 11/11/2019 7:11:25 AM Short time string representation = 7:11 AM