将“ 2018年15月15日上午10:20”转换为日期时间C#

时间:2018-09-16 15:33:37

标签: c# datetime

我有一个REST API,它以字符串形式存储日期时间:  “ 2018年15月15日上午10:20” 以上包括系统语言(在本例中为希伯来语)。有什么办法可以将其转换为C#DateTime

2 个答案:

答案 0 :(得分:0)

如果要将字符串转换为DateTime,则可以使用类似的sintax:

string dateInput = "ספטמבר 15, 2018 בשעה 10:20 am";
DateTime parsedDate = DateTime.Parse(dateInput);

如果您想使用其他文化,请使用:

CultureInfo MyCultureInfo = new CultureInfo("sql-Al");

答案 1 :(得分:0)

我会将所有希伯来语文本转换为英文文本,然后将适当的格式字符串传递给DateTime.TryParseExact

例如

 string dateInEnglish = dateInHebrew.Replace("ספטמבר", "September"). Replace("בשעה","")

从该日期起,将删除所有希伯来语,尽管实际上我会使用正则表达式。

查看TryParseExact上的文档以创建合适的格式字符串:

https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?redirectedfrom=MSDN&view=netframework-4.7.2#System_DateTime_TryParseExact_System_String_System_String_System_IFormatProvider_System_Globalization_DateTimeStyles_System_DateTime__