📅  最后修改于: 2023-12-03 15:07:37.498000             🧑  作者: Mango
在 C# 中,可以使用字符串格式化来将 DateTime 格式化为指定格式的字符串。本篇文章将介绍如何将 04-08-2021 00:00:00 格式的 DateTime 转换为字符串 '08 04 2021'。
DateTime dateTime = DateTime.ParseExact("04-08-2021 00:00:00", "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
string formattedString = dateTime.ToString("dd MM yyyy");
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
DateTime dateTime = DateTime.ParseExact("04-08-2021 00:00:00", "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
string formattedString = dateTime.ToString("dd MM yyyy");
Console.WriteLine(formattedString);
}
}
```csharp
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
DateTime dateTime = DateTime.ParseExact("04-08-2021 00:00:00", "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
string formattedString = dateTime.ToString("dd MM yyyy");
Console.WriteLine(formattedString);
}
}