Home  >  Article  >  Backend Development  >  How to get only the date part from a DateTime object in C#?

How to get only the date part from a DateTime object in C#?

PHPz
PHPzforward
2023-08-28 20:49:02693browse

如何从 C# 中的 DateTime 对象中仅获取日期部分?

There are several ways to get only the date part from a DateTime object.

ToShortDateString() - Converts the value of the current DateTime object to its equivalent short date string representation.

Returns a string containing the current Short date string representation DateTime object.

ToLongDateString() - Converts the value of the current DateTime object to its equivalent long date string representation.

Returns a string containing: the current long date string representation DateTime object.

ToString() - Another way to get a date from a DateTime is to use the ToString() extension method.

The advantage of using ToString() extension method is that we can specify the format The date we want to get.

DateTime.Date - will also remove the time from the DateTime and give us only the date.

The difference between this method and the above example is that this is not a date Convert to string.

Example of using DateTime extension method -

Example

Real-time demonstration

using System;
namespace DemoApplication{
   public class Program{
      public static void Main(){
         var dateTime = DateTime.Now;
         Console.WriteLine($"DateTime Value: {dateTime}");
         var shortDateValue = dateTime.ToShortDateString();
         Console.WriteLine($"Short Date Value: {shortDateValue}");
         var longDateValue = dateTime.ToLongDateString();
         Console.WriteLine($"Long Date Value: {longDateValue}");
         Console.ReadLine();
      }
   }
}

Output

The above program The output is

DateTime Value: 07-08-2020 21:36:46
Short Date Value: 07-08-2020
Long Date Value: 07 August 2020

Example using DateTime.Date -

Example

Real-time demonstration

using System;
namespace DemoApplication{
   public class Program{
      public static void Main(){
         var dateTime = DateTime.Now;
         Console.WriteLine($"DateTime Value: {dateTime}");
         var dateValue = dateTime.Date;
         Console.WriteLine($"Date Value: {dateValue}");
         Console.ReadLine();
      }
   }
}

Output

The output of the above code is

DateTime Value: 07-08-2020 21:45:21
Date Value: 07-08-2020 00:00:00

Example of using ToString() extension method -

Example

Real-time demonstration

using System;
namespace DemoApplication{
   public class Program{
      public static void Main(){
         var dateTime = DateTime.Now;
         Console.WriteLine($"DateTime Value: {dateTime}");
         var dateValue1 = dateTime.ToString("MM/dd/yyyy");
         Console.WriteLine($"Date Value: {dateValue1}");
         var dateValue2 = dateTime.ToString("dd/MM/yyyy");
         Console.WriteLine($"Date Value: {dateValue2}");
         var dateValue3 = dateTime.ToString("d/M/yy");
         Console.WriteLine($"Date Value: {dateValue3}");
         Console.ReadLine();
      }
   }
}

Output

The output of the above code is

DateTime Value: 07-08-2020 21:58:17
Date Value: 08-07-2020
Date Value: 07-08-2020
Date Value: 7-8-20

The above is the detailed content of How to get only the date part from a DateTime object in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete