How to Get the Current Date Without Time in C#
-
C# Program to Get the Current Date Without Time Using
DateTime.Now.ToString()
Method -
C# Program to Get the Current Date Without Time Using
DateTime.Now.Date.ToString()
Method -
C# Program to Get the Current Date Without Time Using
DateTime.Now.ToShortString()
Method -
C# Program to Get the Current Date Without Time Using
DateTime.Now.ToLongString()
Method
In C#, the DateTime
class returns a DateTime
object that shows the value of date and time. DateTime.Now
is used to show the current date and time.
There are some cases where we don’t want to use the date and time together. We only need the current date. In C#, there are several methods that are used to get the current date without the time.
In this article, we are going to get through these methods. Continue reading till the end to find out these methods.
C# Program to Get the Current Date Without Time Using DateTime.Now.ToString()
Method
The method DateTime.Now.ToString()
is used to convert the DateTime
object to a string
of the specific format.
The correct syntax to use this method is as follows:
DateTime.Now.ToString("Format of the date that you want");
Example Code:
using System;
public class DateWithoutTime {
public static void Main() {
string Date = DateTime.Now.ToString("dd-MM-yyyy");
Console.WriteLine("The Current Date Without Time is {0}.", Date);
}
}
Output:
The Current Date Without Time is 13-04-2020.
C# Program to Get the Current Date Without Time Using DateTime.Now.Date.ToString()
Method
The method DateTime.Now.Date.ToString()
is used to fetch the current date. The Date
property is used to fetch the date only with time 00:00:00. To eliminate this time we have used the ToString()
method to get the date in the desired format.
The correct syntax to use this method is as follows:
DateTime.Now.Date.ToString("Format of the date that you want");
Example Code:
using System;
public class DateWithoutTime {
public static void Main() {
var DateAndTime = DateTime.Now;
var Date = dateAndTime.Date.ToString("dd-MM-yyyy");
Console.WriteLine("The Current Date Without Time is {0}.", Date);
}
}
Output:
The Current Date Without Time is 13-04-2020.
C# Program to Get the Current Date Without Time Using DateTime.Now.ToShortString()
Method
The method DateTime.Now.ToShortString()
fetches the date in MM/dd/yyyy
format. This format is set by default.
The correct syntax to use this property is as follows:
DateTime.Now.ToShortString();
Example Code:
using System;
public class DateWithoutTime {
public static void Main() {
var dateAndTime = DateTime.Now;
var Date = dateAndTime.ToShortDateString();
Console.WriteLine("The Current Date Without Time is {0}.", Date);
}
}
Output:
The Current Date Without Time is 4/13/2020.
C# Program to Get the Current Date Without Time Using DateTime.Now.ToLongString()
Method
The method DateTime.Now.ToLongString()
shows the date without time in a long date format. It specifies the day and the month’s name as well.
The correct syntax to use this property is as follows:
DateTime.Now.ToLongString();
Example Code:
using System;
public class DateWithoutTime {
public static void Main() {
var dateAndTime = DateTime.Now;
var Date = dateAndTime.ToLongDateString();
Console.WriteLine("The Current Date Without Time is {0}.", Date);
}
}
Output:
The Current Date Without Time is Monday, April 13, 2020.