37

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");
Vikrant
  • 4,920
  • 17
  • 48
  • 72
Rocshy
  • 3,391
  • 11
  • 39
  • 56
  • 3
    You're taking the current date and time, converting it to a string, parsing it and then converting it to a different string? – Rawling Nov 01 '12 at 14:08
  • Converting to one string format, parsing that and then converting to another string format doesn't make sense. Why not simply do `string test = DateTime.Now.ToString("yyyy-MM-dd")`? – Binary Worrier Nov 01 '12 at 14:19
  • Possible duplicate of [How to get the current date without the time?](https://stackoverflow.com/questions/6817266/how-to-get-the-current-date-without-the-time) – xdtTransform Jun 05 '19 at 08:48

10 Answers10

57

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 ());
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
41
String test = DateTime.Now.ToString("dd.MM.yyy");
Danilo Vulović
  • 2,983
  • 20
  • 31
25

Have you tried

DateTime.Now.Date
mhttk
  • 1,688
  • 1
  • 16
  • 29
  • This isn't actually needed. You can convert a `DateTime` with both a date and a time to a string that has just the date. You don't need to do this first. – Servy Nov 01 '12 at 14:09
  • 3
    it seems to answer the first question: "How can I get the current date but without the time?" – mhttk Nov 01 '12 at 14:11
  • And yet from the context of the question it's clear he wants the result as a string, so you're still going to be calling `ToString` on your `DateTime`, and you'll need to have it provide the exact same formatting that a `DateTime` object with a time would need to supply. In other words, adding `.Date` gains nothing. That's why we read past the first sentence when answering questions. – Servy Nov 01 '12 at 14:12
  • 1
    I understand something different. It seems we are also people: the person asking the question is a she. :) – mhttk Nov 01 '12 at 14:23
  • This is what I was looking for. A date object without the current time. This is better than DateTime.Parse(DateTime.Now.ToString("yyy.MM.dd")) . Also see below, DateTime.Today does the same thing. – efreed Jan 31 '18 at 23:47
11
String test = DateTime.Now.ToShortDateString();
Frank59
  • 3,141
  • 4
  • 31
  • 53
7

it should be as simple as

DateTime.Today
Waqas
  • 317
  • 3
  • 2
6

This should work:

string datetime = DateTime.Today.ToString();
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
MIchael Louw
  • 61
  • 1
  • 1
3

You can get current UTC date without time.

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

KaranSingh
  • 460
  • 4
  • 11
1

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"))
pierroz
  • 7,653
  • 9
  • 48
  • 60
1

Try this:

   var mydtn = DateTime.Today;
   var myDt = mydtn.Date;return myDt.ToString("d", CultureInfo.GetCultureInfo("en-US"));
d0nut
  • 2,835
  • 1
  • 18
  • 23
0

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")
Uriil
  • 11,948
  • 11
  • 47
  • 68
  • You shouldn't be converting a date to a string, parsing it, and then turning it into a string again. Just turn it into the right string to begin with. – Servy Nov 01 '12 at 14:10
  • I know, but he asked, why he gets exception: String was not recognized as a valid DateTime – Uriil Nov 01 '12 at 14:12
  • If you're going to do that you should also solve the problem appropriately, in addition to explaining how to get the incorrect approach to work. – Servy Nov 01 '12 at 14:14