How to get a date in C # today in mm / dd / yyyy format?

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

I need to set a string variable before today's date (preferably without a year), but there should be a better way than creating a month - / - day at a time at a time.

BTW: I'm in the US, so M / dd would be correct, for example. September 11 - September 9.

Note: the answer to the question of kronose came up with a discussion of internationalization, and I thought it was surprising enough since I cannot make it an β€œaccepted” answer.

answer kronoz

+75
date c #
Aug 28 '08 at 16:37
source share
8 answers
+153
Aug 28 '08 at 16:37
source share
β€” -

Not to be terribly pedantic, but if you internationalize the code, it might be more useful to be able to get a short date for a given culture, for example: -

 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; } 

Although it is obvious that the "m / dd / yyyy" approach is far ahead !!

+20
Aug 28 '08 at 17:05
source share
 DateTime.Now.ToString("dd/MM/yyyy"); 
+8
Aug 28 '08 at 16:38
source share

If you want it without a year:

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

DateTime.ToString () has many cool lines:

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

+8
Aug 28 '08 at 16:41
source share
 DateTime.Now.Date.ToShortDateString() 

is culture specific.

Best to stick with:

 DateTime.Now.ToString("d/MM/yyyy"); 
+7
Aug 28 '08 at 16:41
source share
 string today = DateTime.Today.ToString("M/d"); 
+6
Aug 28 '08 at 16:42
source share
 DateTime.Now.Date.ToShortDateString() 

I think this is what you are looking for

+3
Aug 28 '08 at 16:39
source share

Or without a year:

 DateTime.Now.ToString("M/dd") 
+3
Aug 28 '08 at 16:41
source share



All Articles