DateTime.ToShortDateString() Method in C#


The DateTime.ToShortDateString() method in C# is used to convert the value of the current DateTime object to its equivalent short date string representation.

Syntax

Following is the syntax −

public string ToShortDateString ();

Example

Let us now see an example to implement the DateTime.ToShortDateString() method −

using System;
public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 08, 11, 9, 10, 45);
      Console.WriteLine("Date = {0}", d);
      string str = d.ToShortDateString();
      Console.WriteLine("Short date string representation = {0}", str);
   }
}

Output

This will produce the following output −

Date = 8/11/2019 9:10:45 AM
Short date string representation = 8/11/2019

Example

Let us now see another example to implement the DateTime.ToShortDateString() method −

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.ToShortDateString();
      Console.WriteLine("Short date string = {0}", pattern.ShortDatePattern);
      Console.WriteLine("Short date string representation = {0}", str);
   }
}

Output

This will produce the following output −

Date = 10/16/2019 8:56:40 AM
Current culture = en-US
Short date string = M/d/yyyy
Short date string representation = 10/16/2019
raja
Published on 08-Nov-2019 11:07:53
Advertisements