How do I get today's date in C# in mm/dd/yyyy format?

C#Date

C# Problem Overview


How do I get today's date in C# in mm/dd/yyyy 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 one piece at a time.

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][1]

[1]: https://stackoverflow.com/questions/32747/how-do-i-get-todays-date-in-c-in-8282008-format#32819 "kronoz's answer"

C# Solutions


Solution 1 - C#

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

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

Solution 2 - C#

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!!

Solution 3 - C#

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

Solution 4 - C#

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

Solution 5 - C#

DateTime.Now.Date.ToShortDateString()

is culture specific.

It is best to stick with:

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

Solution 6 - C#

string today = DateTime.Today.ToString("M/d");

Solution 7 - C#

DateTime.Now.Date.ToShortDateString()

I think this is what you are looking for

Solution 8 - C#

Or without the year:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKengView Question on Stackoverflow
Solution 1 - C#Joel CoehoornView Answer on Stackoverflow
Solution 2 - C#ljsView Answer on Stackoverflow
Solution 3 - C#Corin BlaikieView Answer on Stackoverflow
Solution 4 - C#FlySwatView Answer on Stackoverflow
Solution 5 - C#Corin BlaikieView Answer on Stackoverflow
Solution 6 - C#Billy JoView Answer on Stackoverflow
Solution 7 - C#Josh MeinView Answer on Stackoverflow
Solution 8 - C#EBGreenView Answer on Stackoverflow