tags:

views:

8273

answers:

13

How do i get today's date in C# in 8/28/2008 format? I need to set a string variable to today's date (preferably without the year) but there's got to be a better way than building it month-/-day once piece at a time.

Edit: BTW: I'm in the US so M/dd would be correct e.g. September 11th is 9/11.

Note: an answer from kronoz came in that discussed internationalization and I thought it was awesome enough to mention since I can't make it an 'accepted' answer as well.

kronoz's answer

+13  A: 

DateTime.Now.ToString("M/d/yyyy");

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Joel Coehoorn
Updated with link to string formatting reference
Joel Coehoorn
+1  A: 

DateTime.Now.ToString("dd/MM/yyyy");

Edit: Damn people reply fast on stackoverflow!

Corin
A: 

DateTime.Now.Date.ToShortDateString()

I think this is what you are looking for

jmein
+1  A: 
DateTime.Now.Date.ToShortDateString()

is culture specific.

best to stick with

DateTime.Now.ToString("d/MM/yyyy");
Corin
+2  A: 

Or without the year:

DateTime.Now.ToString("M/dd")

EBGreen
+5  A: 

If you want it without the year:

DateTime.Now.ToString("MM/DD");

DateTime.ToString() has a lot of cool format strings:

http://msdn.microsoft.com/en-us/library/aa326721.aspx

FlySwat
+2  A: 
string today = DateTime.Today.ToString("M/d");
Bill Ayakatubby
+6  A: 

Not to be horribly pedantic, but if you are internationalising the code it might be more useful to have the facility to get the short date for a given culture, e.g.:-

using System.Globalization;
using System.Threading;

...

var currentCulture = Thread.CurrentThread.CurrentCulture;
try {
  Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-us");
  string shortDateString = DateTime.Now.ToShortDateString();
  // Do something with shortDateString...
} finally {
  Thread.CurrentThread.CurrentCulture = currentCulture;
}

Though clearly the "m/dd/yyyy" approach is considerably neater!!

kronoz
A: 

@[kronoz]

you and your type are the reason SO will become the number 1 resource for programmers who want to get things done!

I wish I could accept two answers but for now all I can do is hope you get voted up enough to always be easily visible.

THANKS!!!!

BTW: I can also link to you in the question. 90)

Keng
A: 

I'm sure I'll be downvoted for saying this, but why didn't you just consult your documentation... or google?

Dan
A: 

@[Dan]

His Response

Ummmm.....maybe you don't fully understand the point of SO. They want Google to point to SO for these types of answers. Do you think there are any questions asked here that couldn't be answered by searching Google?

I won't down vote your answer because it'll ding me as well but I think if you keep responding to questions with, "why didn't you just consult your documentation... or google?" your life at SO will be very difficult (given that you can do more things as you gain more rep).

Of course, if you edit your answer, I'll come back and adjust this as well.

Thanks.

Ken

Keng
A: 

@Ken

  1. It only costs you a point per down vote so unless you're super concerned about your e-cred I wouldn't let it keep you from down voting me if you feel it's justified.

  2. Yes I do think there are questions that can't be answered by google:

    • Questions that take more than a few words to ask,
    • Questions that deal with lesser-used tools, or a combination of tools,
    • Questions that are plain old hard to google for (ie. barenaked ladies)
  3. Shouldn't we use stackoverflow in the way that makes the most sense, and not the way that will make Jeff & Joel the most money? Jeff wants google to point to SO for every programming question because it'll bring him ad revenue. I want google to point to SO for interesting/difficult questions because that'll actually be a useful resource.

  4. And finally, I'm not here to gain SO-points. I'm here to participate in an interesting idea. I will not censor myself to make your recursive back-patting easier. If that makes my "life at SO" more difficult then so be it :-/

Basically my point is that I don't think SO is the right place to ask questions like "how do I format a date in C#" or "how do I get the size of a file in C/C++". Those are trivially answered questions and it seems like me that people only ask them to get more points and not actually out of genuine want.

edit: s/are want/want

Dan
A: 

To Dan (Since SO's still in beta I feel we have some latitude in going off-topic here)

Dan's Response

Response to each of your points by numbers:

  1. well, I am concerned about my rep score and about my badge count. dinging you on that would be shooting myself in the foot because to get to the place where I want to be here in SO, I need a high score. As Jeff said, the neg to the voter's score makes them think twice about whether or not to down-check the response/question.

  2. If the question can't be answered searching Google then the odds are even worse that they'll be answered here till Google get the go-ahead to start searching.

  3. If you listen to the podcasts, you'll hear why the set this up. It wasn't to make more money; it was to create a repository of info that the community can keep up to date because the guy that gave the answer to "How do you assign variable arrays in MUMPS using Cache Objects?" didn't realize/care that 8 months after he gave that answer, it changed completely and that method no longer is valid and didn't update his blog. But here, id someone were to find that answer and try it and it breaks, he can find out why and then update the answer so everyone is not being given the wrong info. It seems, from your answer, that you want to use this only in a way that keeps SO from making money and not what would be helpful. Let take my question for example. Joel did a very nice job of answering it and kronoz did an extraordinarily good job pointing out that, although the answer is concise and correct, programmers who have international clientele may want to use a differ method. I updated my question to point that out at the very outset so that no matter how far down the next person reads, they'll be told about that facet. Just because it makes them money is no reason to keep from using it like this. You, in fact, weren't bothered at all by the hundreds of other questions on this site that 'should' (to hear you view point on the subject) have just looked at the documentation or Googled it rather than post such a mundane (read: n008) question. Your interesting and difficult questions will still be there; there's room. And with the rss tag feed, all the experts in Opalis job sequence programing will still see the questions when they are posted.

  4. Well, I am here to gain rep points because I have to to participate at an important level. If I'm going to use it in the way that makes the most sense, I'm going to need points to help organize it, edit it, and add to it. If you want it to have all these difficult and interesting questions, you're going to need points too. You need a minimum of 15 just to vote on questions and answers. Without at least that much, you'll never be able to help the difficult and interesting questions get the attention you think the deserve and SO will be the worse for it.

And to speak to the implied accusation that I only asked the question to get rep and "not actually out of genuine want", I can say here in front of God and everyone that the answer to that question actually did help me complete an assignment from my boss concerning a C# email process that I've been asking about at various levels of completion here is SO. So if your beef with the question is: that question wasn't difficult for you, or the question wasn't interesting to you, or the question was just to make Google point to SO so Joel can buy another (insert expensive item here), but that you say I asked it just to get rep, well then, your wrong and so we've wasted a lot of time going round and round only now getting to your point only to discover that you were wrong.

I hope this doesn't come off snide or condescending because that really isn't what I'm trying to do. I'm just trying to take each point in turn and explain why I don't think it is the way you see it, or at least, there may be another way to see it in which this question is both valid and useful for me and SO....maybe not to you but to me and SO at least.

Ken

Keng