Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current date without time

Tags:

c#

How can I get the current date but without the time? I am trying to convert the date from the "dd.mm.yyyy" format to "yyyy-MM-dd", because DateTime.Now returns the time too, I get an error (String was not recognized as a valid DateTime.) when I try to do the following.

string test = DateTime.ParseExact(DateTime.Now.ToString(), "dd.MM.yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd"); 
like image 330
Rocshy Avatar asked Nov 01 '12 14:11

Rocshy


People also ask

How can I get the current date without time?

Using Calendar. One of the most common ways to get a Date without time is to use the Calendar class to set the time to zero. By doing this we get a clean date, with the time set at the start of the day. As we can see, it returns a full date with the time set to zero, but we can't ignore the time.


1 Answers

Use the Date property: Gets the date component of this instance.

var dateAndTime = DateTime.Now; var date = dateAndTime.Date; 

variable date contain the date and the time part will be 00:00:00.

or

Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy")); 

or

DateTime.ToShortDateString Method-

Console.WriteLine(DateTime.Now.ToShortDateString ()); 
like image 101
Pranay Rana Avatar answered Oct 02 '22 11:10

Pranay Rana