Rocshy
Rocshy

Reputation: 3509

Current date without time

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");

Upvotes: 39

Views: 139757

Answers (10)

KaranSingh
KaranSingh

Reputation: 610

You can get current UTC date without time.

string currentDate = DateTime.UtcNow.ToString("yyyy-MM-dd");

Upvotes: 3

MIchael Louw
MIchael Louw

Reputation: 61

This should work:

string datetime = DateTime.Today.ToString();

Upvotes: 6

John Farragher
John Farragher

Reputation: 11

Try this:

   var mydtn = DateTime.Today;
   var myDt = mydtn.Date;return myDt.ToString("d", CultureInfo.GetCultureInfo("en-US"));

Upvotes: 1

pierroz
pierroz

Reputation: 7870

As mentioned in several answers already that have been already given, you can use ToShorDateString():

DateTime.Now.ToShortDateString();

However, you may be a bit blocked if you also want to use the culture as a parameter. In this case you can use the ToString() method with the "d" format:

DateTime.Now.ToString("d", CultureInfo.GetCultureInfo("en-US"))

Upvotes: 1

Waqas
Waqas

Reputation: 317

it should be as simple as

DateTime.Today

Upvotes: 7

Uriil
Uriil

Reputation: 12618

If you need exact your example, you should add format to ToString()

    string test = DateTime.ParseExact(DateTime.Now.ToString("dd.MM.yyyy"), "dd.MM.yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");

But it's better to use straight formatting:

    string test = DateTime.Now.ToString("yyyy-MM-dd")

Upvotes: 0

Frank59
Frank59

Reputation: 3261

String test = DateTime.Now.ToShortDateString();

Upvotes: 11

Pranay Rana
Pranay Rana

Reputation: 176886

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 ());

Upvotes: 58

mhttk
mhttk

Reputation: 1696

Have you tried

DateTime.Now.Date

Upvotes: 25

Danilo Vulović
Danilo Vulović

Reputation: 3063

String test = DateTime.Now.ToString("dd.MM.yyy");

Upvotes: 42

Related Questions