我如何用mm / dd / yyyy格式的C#获取今天的日期?

时间:2023-01-14 14:33:11

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

我如何用mm / dd / yyyy格式的C#获取今天的日期?

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.

顺便说一句:我在美国,所以M / dd是正确的,例如9月11日是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的答案来自于讨论的国际化,我认为它非常棒,因为我不能把它作为一个“接受”的答案。

kronoz's answer

8 个解决方案

#1


173  

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

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

#2


22  

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

虽然显然“m / dd / yyyy”的方法相当简洁!

#3


11  

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

#4


8  

If you want it without the year:

如果你想要没有年份:

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

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

DateTime.ToString()有很多很酷的格式字符串:

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

#5


7  

DateTime.Now.Date.ToShortDateString()

is culture specific.

是文化特定的。

It is best to stick with:

最好坚持:

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

#6


6  

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

#7


3  

Or without the year:

或者没有年份:

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

#8


3  

DateTime.Now.Date.ToShortDateString()

I think this is what you are looking for

我想这就是你要找的东西

#1


173  

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

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

#2


22  

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

虽然显然“m / dd / yyyy”的方法相当简洁!

#3


11  

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

#4


8  

If you want it without the year:

如果你想要没有年份:

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

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

DateTime.ToString()有很多很酷的格式字符串:

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

#5


7  

DateTime.Now.Date.ToShortDateString()

is culture specific.

是文化特定的。

It is best to stick with:

最好坚持:

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

#6


6  

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

#7


3  

Or without the year:

或者没有年份:

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

#8


3  

DateTime.Now.Date.ToShortDateString()

I think this is what you are looking for

我想这就是你要找的东西