-1

I'm converting a string to a date format using DateTime.Parse(). My original string consists of the date only but when i use DateTime.Parse() it adds the time to it as well giving me 01/12/2000 00:00:00. I only want the date 01/12/2000. Is there any other way to simply just get the date?

  • 1
    A `DateTime` always contains the time portion, it just has a value. If you want to display it without time, easy: `dt.ToShortDateString` or `dt.ToString("d")` – Tim Schmelter May 22 '17 at 14:42
  • 1
    Why? You only need that for **display purposes**. Until you have to display it, leave it alone. – DigiFriend May 22 '17 at 14:43
  • If you truely want a type that does not have time then you need a 3rd party library like [Noda Time](http://nodatime.org/). – Igor May 22 '17 at 14:43
  • What's the *actual problem* you're trying to solve? The `DateTime` object contains the data you parsed. And, yes, it also has other fields which you didn't set to anything. But what's the problem? – David May 22 '17 at 14:45

1 Answers1

0
DateTime dt = DateTime.Parse("01/12/2000");
Console.WriteLine(dt.ToString("dd/MM/yyyy"));

DateTime always has a underlying Time fraction, it's just the way you define to show it that makes it look like that. So the tostring function can be used with the given formatting.

If you really want it not to have a time (and i wouldn't know why you'd want that) there are 3th party addons available.

Jelman
  • 734
  • 6
  • 17
  • 1
    Or simply use `ToShortDateString()`. – BWA May 22 '17 at 14:46
  • I know, but i tought it would be more clear to the person asking , if I showed it with formatting, as this can be changed to other needs :) – Jelman May 22 '17 at 14:47