gpt4 book ai didi

c# - 使用精确解析转换英文日期

转载 作者:太空宇宙 更新时间:2023-11-03 23:06:38 24 4
gpt4 key购买 nike

我尝试将英文日期转换为德文日期,但我的格式不正确。

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
DateTime currentCultureDate = DateTime.Now;
string format = "dd.MM.yyyy HH:mm:ss";

Console.WriteLine("Format: " + format);
Console.WriteLine("Original Date: " + currentCultureDate);

DateTime convertedDate = DateTime.ParseExact(currentCultureDate.ToString(), format, new CultureInfo("de-DE"));

Console.WriteLine("Converted Date: " + convertedDate);

格式异常.....

最佳答案

DateTime.ParseExact用于从 string 创建 DateTime。您可以传递 DateTimeFormatCultureInfo,它们将用于将该字符串转换为 DateTime

该方法不会将其转换为另一个CultureInfo 中的字符串,比如de-DE。因此你可以使用 DateTime.ToString :

string germanFormat = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss", new CultureInfo("de-DE"));

关于c# - 使用精确解析转换英文日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40971778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com