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

PHPz
Release: 2023-08-28 20:49:02
forward
692 people have browsed it

如何从 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();
      }
   }
}
Copy after login

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
Copy after login

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();
      }
   }
}
Copy after login

Output

The output of the above code is

DateTime Value: 07-08-2020 21:45:21
Date Value: 07-08-2020 00:00:00
Copy after login

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();
      }
   }
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template