Get current date without time in C#
This post will discuss how to get the current date in UTC without time in C#.
The DateTime.UtcNow
property returns a DateTime
object whose value is the current UTC date and time. To get the date component of DateTime.Now
, we can use any of the following methods:
1. Using DateTime.ToString()
method
The DateTime.ToString() method can be used to get the string representation of the current DateTime
object in a specified format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string date = DateTime.UtcNow.ToString("MM-dd-yyyy"); Console.WriteLine("The current date is {0}", date); } } /* Sample Output: The current date is 01-07-2020 */ |
Alternatively, we can use the Short Date d
format specifier, which displays the current DateTime
object value using a short date pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string date = DateTime.UtcNow.ToString("d"); Console.WriteLine("The current date is {0}", date); } } /* Sample Output: The current date is 07-01-2020 */ |
We can also use the Long Date D
format specifier that displays the current DateTime
object value using a long date pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string date = DateTime.UtcNow.ToString("D"); Console.WriteLine("The current date is {0}", date); } } /* Sample Output: The current date is 06 January 2020 */ |
2. Using DateTime.ToShortDateString()
method
The DateTime.ToShortDateString() method can be used to convert the value of the current DateTime
object to its equivalent short date string representation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string date = DateTime.UtcNow.ToShortDateString(); Console.WriteLine("The current date is {0}", date); } } /* Sample Output: The current date is 07-01-2020 */ |
3. Using DateTime.ToLongDateString()
method
The DateTime.ToLongDateString() method can be used to convert the value of the current DateTime
object to its equivalent long date string representation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string date = DateTime.UtcNow.ToLongDateString(); Console.WriteLine("The current date is {0}", date); } } /* Sample Output: The current date is 06 January 2020 */ |
4. Using DateTime.GetDateTimeFormats()
method
The DateTime.GetDateTimeFormats() method returns an array of all the string representations of the current DateTime
object supported by the specified format specifier. The idea is to pass the d
format specifier to the method and print the first value in the returned array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; public class Example { public static void Main() { string date = DateTime.UtcNow.GetDateTimeFormats('d')[0]; Console.WriteLine("The current date is {0}", date); } } /* Sample Output: The current date is 07-01-2020 */ |
That’s all about getting the current date without time in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)