問題描述
如何在 C# 中轉換 dateTime 格式? (How do I convert dateTime format in C#?)
這是
string date = "07‑Feb‑2020";
我不確定如何將其轉換為
"2020‑02‑07 00:00:00"
表示形式
參考解法
方法 1:
ParseExact
into DateTime
then format to the desired string
representation:
string date = "07‑Feb‑2020";
string result = DateTime
.ParseExact(date, "dd‑MMM‑yyyy", CultureInfo.InvariantCulture)
.ToString("yyyy‑MM‑dd HH:mm:ss");
方法 2:
Use Parse Exact
DateTime.ParseExact(date, "dd‑MMM‑yyyy", null);
方法 3:
Use DateTime:
DateTime thisDate1 = new DateTime(2011, 6, 10);
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".");
DateTimeOffset thisDate2 = new DateTimeOffset(2011, 6, 10, 15, 24, 16,
TimeSpan.Zero);
Console.WriteLine("The current date and time: {0:MM/dd/yy H:mm:ss zzz}",
thisDate2);
// The example displays the following output:
// Today is June 10, 2011.
// The current date and time: 06/10/11 15:24:16 +00:00
You can create any formatting you wish to do
(by Nawaf Momin、Dmitry Bychenko、Atk、Lukáš Machajdík)