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");
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.
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 ());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With