-2
BirthDate = new DateTime(2015,12,30, 00, 0, 0);
// and Tried this too
//BirthDate = new DateTime(2015,12,30);
Console.WriteLine(BirthDate);

The result is this:

12/30/2015 12:00:00 AM

What I want to achieve is:

12/30/2015 00:00:00

or:

12/30/2015

Is there any way I could do this in the console app using the DateTime?

[EDIT]

I know how to Format Strings in Console.WriteLine method or manipulate the strings. What i asked here was whether there is a method in DateTime class to do this.

aimme
  • 5,067
  • 4
  • 42
  • 54
  • 3
    Let, DateTime expert, Jon Skeet handle this question ;) – Neel Sep 03 '15 at 11:15
  • Do you mean `BirthDate.ToString("MM/dd/yyyy")`? @Neel you don't need to be a genius to figure out some date formatting string. – CodeCaster Sep 03 '15 at 11:17
  • Dates do not have formats. Are you asking how to *display* the date in a specific way? – Panagiotis Kanavos Sep 03 '15 at 11:19
  • Agree! but It was instant reaction while I saw datetime tag before even reading the question @CodeCaster – Neel Sep 03 '15 at 11:22
  • what's wrong with this code?! – Arash Sep 03 '15 at 11:23
  • How come no-one remembered that `Console.WriteLine` accepts a format string? – Panagiotis Kanavos Sep 03 '15 at 11:26
  • 1
    Is this _really_ duplicate? None of answers in duplicate link answers this question as far as I can see.. or.. I need more coffee to wake up. – Soner Gönül Sep 03 '15 at 11:30
  • @SonerGönül not duplicate of *that* question but I'm certain there are at least 5 other questions that ask how to output a date in a certain way – Panagiotis Kanavos Sep 03 '15 at 11:31
  • `12:00:00 AM` is the same as `00:00:00`, only displayed in a different culture. Compare `Console.WriteLine(BirthDate.ToString(CultureInfo.InvariantCulture));` to `Console.WriteLine(BirthDate.ToString(CultureInfo.GetCultureInfo("en-US")));` -- When displaying a `DateTime` it's best (read: most unambiguous) to always specify a format and a formatProvider (i.e. a CultureInfo). So try: `BirthDate.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)`. – Corak Sep 03 '15 at 11:32
  • @Corak in this case the only culture-sensitive part is the separator, all others are numbers. In Germany the separator used to be `.` (I think) – Panagiotis Kanavos Sep 03 '15 at 11:34
  • @PanagiotisKanavos - the char `'/'` is the global date separator (at least by default?). So `new DateTime(2015, 12, 30).ToString("MM/dd/yyyy", CultureInfo.GetCultureInfo("de-DE"))` will produce `"12.30.2015"` and a lot of strange looks from germans (we use the "proper" format: `dd.MM.yyyy`). -- joking aside, `yyyy-MM-dd` is the only true format and should be used everywhere instead of everything else. – Corak Sep 03 '15 at 11:39
  • @Soner what about the [second answer](http://stackoverflow.com/a/6124299/266143) to the one I closed this as a duplicate of? The core question here is _"How to format a date"_, which is answered there. I hope you don't mean _"But the format here is `MM/dd/yyyy`, while in that answer it is `dd/MM/yyyy`"_, we don't go create a question per date format. I find the one I linked better than the one currently linked, because the latter doesn't even mention dates. _"How to format strings in Console.WriteLine"_ is a different question. – CodeCaster Sep 03 '15 at 11:51
  • @PanagiotisKanavos I reopened the question and posted my answer which I try to explain a little bit more why OP get this result at the first time and how to get the other formats that he wants. After that, I closed it again if you don't mind. – Soner Gönül Sep 03 '15 at 11:51
  • We already have hundreds, if not thousands of questions about formatting dates. Getting a bit tired of the repetition. – CodeCaster Sep 03 '15 at 11:52
  • @SonerGönül not at all - just wish I could find the *right* duplicate for date related questions easily so I don't have to search each time! – Panagiotis Kanavos Sep 03 '15 at 11:55
  • @CodeCaster You have a point. I wasn't look the second answer, and honestly, your duplicate question is not even clear in my opinion. It says; _I can not use any string conversion methods as I need the date in object form_ and I'm like o.O? What is that _date in object form_ exactly? Does he wanna get a DateTime without Time part (which is not possible as we all know) or does he wanna get only only date part with midnight time part (which is `.Date` property for) or does he wanna get a string representation which includes only date part? But of course, as you said, we should close... – Soner Gönül Sep 03 '15 at 12:00
  • @CodeCaster all of these questions as a duplicate as "_How to format a date?_" one which we have thousands (maybe millions in future) of questions like that.. – Soner Gönül Sep 03 '15 at 12:01
  • @Soner, Panagiotis because of all this duplication, the Google query "C# print format date only datetime site:stackoverflow.com" yields 36.000 (!) results. Subtract some duplication because of tag pages and we easily have 20.000 questions about date formatting, which is extremely sad. We need a very good, canonical date formatting question that explains what a DateTime is and how to get only the parts you're interested out of it in the desired format, but I'm afraid it already exists but is unfindable. – CodeCaster Sep 03 '15 at 12:03
  • @CodeCaster Amen to that. Maybe because one of the reason that people can't find the right link in Google search can be there are too many variation of a DateTime formatting. – Soner Gönül Sep 03 '15 at 12:13
  • I know how to Format Strings in Console.WriteLine method or manipulate the strings. What i asked here was whether there is a method in DateTime class to do this. – aimme Sep 03 '15 at 15:19
  • @aimme I think that was answered in this answer already, multiple times. In fact, it's the same thing as WriteLine - use the proper format string either in `ToString` or with String.Format, WriteLine etc. – Panagiotis Kanavos Sep 04 '15 at 07:11

7 Answers7

3

Try this

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

As mentioned by Panagiotis Kanavos in comments, you can format it as well in this way

Console.WriteLine("{0:MM/dd/yyyy}", BirthDate);
NASSER
  • 5,610
  • 7
  • 32
  • 54
3

There are already right answers here but let me try to explain a little bit deeper..

Since there is no Console.WriteLine(DateTime) overload, your code will call Console.WriteLine(object) overload and for DateTime type, this method will generate G standard format specifier representation of your CurrentCulture.

That means the combination of your ShortDatePattern and LongTimePattern properties is MM/dd/yyyy hh:mm:ss tt of your CurrentCulture.

And from this result you get, your CurrentCulture has / as a DateSeparator and : as a TimeSeparator which is great for the results you want. You will not need to use another IFormatProvider to generate exact results because of these separators.

Just format your value with custom date and time format specifiers like;

Console.WriteLine("{0:MM/dd/yyyy HH:mm:ss}", BirthDate); // 12/30/2015 00:00:00
Console.WriteLine("{0:MM/dd/yyyy}", BirthDate); // 12/30/2015

or use DateTime.ToString() method which is equal;

Console.WriteLine(BirthDate.ToString("MM/dd/yyyy HH:mm:ss")); // 12/30/2015 00:00:00
Console.WriteLine(BirthDate.ToString("MM/dd/yyyy");// 12/30/2015
Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
0

Converts the value of the current DateTime object to its equivalent string representation using the specified format and the formatting conventions of the current culture.

Console.WriteLine(BirthDate.ToString("MM/dd/yyyy");
Nalaka
  • 1,145
  • 7
  • 12
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Mihai Dinculescu Sep 03 '15 at 11:49
  • @Mihai-AndreiDinculescu thanks.. I agree with you... updated – Nalaka Sep 03 '15 at 11:54
  • @Mihai don't flag valid, but poor answers as "not an answer". That flag will be denied as it is at least an _attempt_ at answering the question. Use downvotes for poor answers. – CodeCaster Sep 03 '15 at 11:56
0

By using the date property, for example:

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

The date variable will contain the date, then the time will show 00:00:00..

0

You may refer Standard Date and Time Format Strings for vast options. msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx.

And for your requirement, you may do something like the example below. (which were mentioned already by others in this thread)

BirthDate = new DateTime(2015,12,30, 0, 0, 0);
// and Tried this too
//BirthDate = new DateTime(2015,12,30);
// 12/30/2015 00:00:00
Console.WriteLine(BirthDate.ToString("MM/dd/yyyy HH:mm:ss"));
// or
// 12/30/2015
Console.WriteLine(BirthDate.ToString("MM/dd/yyyy"));
ken lacoste
  • 859
  • 8
  • 21
-1
     var datetime = BirthDate.Date.ToShortDateString();
     Console.WriteLine(datetime);
Arash
  • 861
  • 7
  • 17
  • the time value of BirthDate is already 00:00:00 when you set `BirthDate = new DateTime(2015,12,30, 00, 0, 0);` there is no need to use another variable – fubo Sep 03 '15 at 11:23
  • `BirthDate.Date` is enough, – NASSER Sep 03 '15 at 11:24
  • X-TECH who said that BirthDate.Date is enough.check the code then down vote me!!I have tested the code – Arash Sep 03 '15 at 11:26
  • This won't work in non-US locales, or even in the US if the user changes the system's short date pattern. Moreover, the short date string doesn't include a time component so even the call to `.Date` has no effect. – Panagiotis Kanavos Sep 03 '15 at 11:29
-1

Another way of doing this without the hard coding of format strings would be:

    var currentCulture = CultureInfo.CurrentCulture;
    var shortDatePattern = currentCulture.DateTimeFormat.ShortDatePattern;
    Console.WriteLine(DateTime.Today.ToString(shortDatePattern));
Sean Duffy
  • 11
  • 4
  • The short date pattern is locale *and* user-specific. If you don't want to specify the format exactly, you should specify the short date pattern *and* the corresponding culture, ie en-US in this case – Panagiotis Kanavos Sep 03 '15 at 11:37
  • So, like this? `Console.WriteLine(DateTime.Today.ToString(shortDatePattern, currentCulture));` – Sean Duffy Sep 03 '15 at 20:33