
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to get only Date portion from DateTime object in C#?
There are several ways to get only date portion from a DateTime object.
ToShortDateString() − Converts the value of the current DateTime object to its equivalent short date string representation.
Returns a string that contains the short date string representation of the current DateTime object.
ToLongDateString() − Converts the value of the current DateTime object to its equivalent long date string representation.
Returns a string that contains the long date string representation of the current DateTime object.
ToString() − One more way to get the date from DateTime is using ToString() extension method.
The advantage of using ToString() extension method is that we can specify the format of the date that we want to fetch.
DateTime.Date − will also remove the time from the DateTime and provides us the Date only.
The difference of this method from the above example is that, here the date is not converted to a string.
Example using extensions methods of DateTime −
Example
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 output of the above program 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
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 using ToString() extension method −
Example
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
- Related Articles
- Get only the date from datetime in MySQL?
- How to select only MySQL date from datetime column?
- MySQL query to get current datetime and only current date
- How to update only day portion of MySQL Date?
- MySQL query to get only the minutes from datetime?
- How to extract from datetime column in MySQL by comparing only date and ignoring whitespace?
- How to extract only the month and day from a datetime object in Python?
- How to compare DateTime Column with only Date not time in MySQL?
- Extracting only date from datetime field in MySQL and assigning it to PHP variable?
- How to part DATE and TIME from DATETIME in MySQL?
- MySQL Query to convert from datetime to date?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- How to create JavaScript Date object from date string?
- How to convert date to datetime in Python?
- Create DATETIME from DATE and TIME in MySQL?
