C# में DateTime.ToShortTimeString () विधि का उपयोग वर्तमान डेटटाइम ऑब्जेक्ट के मान को उसके समकक्ष शॉर्ट टाइम स्ट्रिंग प्रतिनिधित्व में बदलने के लिए किया जाता है।
सिंटैक्स
निम्नलिखित वाक्य रचना है -
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
उदाहरण
आइए अब डेटटाइम को लागू करने के लिए एक और उदाहरण देखें। 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