DateTime转换在c#
我有 DateTime
此格式的变量dd / mm / yyyy hh:mm:ss
。
我想将其转换为mm / dd / yyyy hh:mm:ss
。 (月日)
我试图使用DateTime.parse这样:
I have DateTime
variable in this format "dd/mm/yyyy hh:mm:ss"
.
I would like to convert this to "mm/dd/yyyy hh:mm:ss"
. (Month before day)
I tried to use DateTime.parse like this:
DateTime dt = new DateTime();
dt = dateTimePicker1.Value;
dt = DateTime.Parse(dt.ToString(), "mm/dd/yyyy", null);
但它不工作。
我也尝试使用 DateTime.ParseExact()
,但仍然没有发生。
but it wont work.
I also try it with DateTime.ParseExact()
but still nothing happened.
任何建议?
A DateTime
没有 em>隐式格式。它只是具有日期和时间值。
A DateTime
doesn't have any implicit format. It just has date and time values.
只有格式化主题 适用于您尝试获取文字表示。如果你想得到它的字符串表示,你可以使用 .ToString()
方法,如;
Formatting subject only applies when you try to get it's textual representation. If you wanna get string represetation of it, you can use .ToString()
method like;
string s = dateTimePicker1.Value.ToString("MM/dd/yyyy hh:mm:ss",
CultureInfo.InvariantCulture);
请记住, mm
说明符是分钟, MM
说明符是几个月。另外 hh
说明符用于 12-小时时钟和 HH
说明符是 24小时制 表示。您可以想使用 HH
。
And remember, mm
specifier is for minutes, MM
specifier is for months. Also hh
specifier is for 12-hour clock and HH
specifier is for 24-hour clock representations. You might wanna use HH
instead.
我使用了 InvariantCulture
作为第二个参数,因为 /
和:
具有特殊的含义,替换我当前文化或提供的文化日期或时间分隔符。
I used InvariantCulture
as a second parameter because /
and :
have special meaning as replace me current culture or supplied culture date or time separator.