Cultureinfo和error ="字符串未被识别为有效日期时间“在Windows 10和Windows 7之间 [英] Cultureinfo and error ="string was not recognized as a valid datetime" between windows 10 and windows 7

查看:113
本文介绍了Cultureinfo和error ="字符串未被识别为有效日期时间“在Windows 10和Windows 7之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 10和Visual Studio 2017中运行此代码并且它正常工作,但是当我在Windows 7和Visual Studio 2015 Update 3中运行此代码时,我收到错误:



字符串未被识别为有效的DateTime....在此行中

I run This Code In windows 10 and Visual studio 2017 and it work Correctly, But when i Run This Code in windows 7 and Visual Studio 2015 Update 3 i Get Error :

"String was not recognized as a valid DateTime" .... in this line

DateTime dt = DateTime.Parse(s, Thread.CurrentThread.CurrentCulture.DateTimeFormat);





我如何解决它:????



我的代码:



How Can i Solved It :????

My Code:

CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("fa-IR");
string s = "1396/02/31";
DateTime dt = DateTime.Parse(s, Thread.CurrentThread.CurrentCulture.DateTimeFormat);
Console.WriteLine(dt.ToShortDateString());
Thread.CurrentThread.CurrentCulture = originalCulture;
Console.ReadKey();
//error=String was not recognized as a valid DateTime.





我尝试过:



这个链接我发现

https://msdn.microsoft.com/en-us/library/bb762911(v=vs.110).aspx

推荐答案

Windows 10增加了对Jalali日历的支持。

因此, DateTimeFormatInfo类(System.Globalization) [ ^ ]类应返回不同的日期格式字符串 fa-Ir在Windows 10及更早版本上调用时(无法在此测试)。



首先,你必须注意到你的日期字符串是Jalali而日期时间对象使用公历。



要从Jalali日期获取格里高利日期,请使用 ToDateTime 方法 PersianCalendar类(System.Globalization) [< a href =https://msdn.microsoft.com/en-us/library/system.globalization.persiancalendar(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]。



但是在转换之前你必须解析字符串。想到的第一种方法是使用固定格式yyyy / MM / dd的 DateTime.Parse 。但这将在特定日期失败。因此,您必须将字符串拆分为年,月和日部分。



未经测试的示例,不进行解析错误检查:

Windows 10 added support for the Jalali calendar.
As a result, the DateTimeFormatInfo Class (System.Globalization)[^] class should return different date format strings for "fa-Ir" when called on Windows 10 and older versions (can not test it here).

At first you have to observe that your date string is Jalali while DateTime objects use the Gregorian calendar.

To get the Gregorian date from a Jalali date use the ToDateTime method of the PersianCalendar Class (System.Globalization)[^].

But before that conversion you have to parse the string. The first approach coming into mind is using DateTime.Parse with the fixed format "yyyy/MM/dd". But this will fail for specific dates. So you have to split the string into the year, month, and day parts.

Untested example without parse error checking:
String[] dateParts = s.Split('/');
int year = int.Parse(dateParts[0]);
int month = int.Parse(dateParts[1]);
int day = int.Parse(dateParts[2]);
PersianCalendar pc = new PersianCalendar();
DateTime dt = pc.ToDateTime(year, month, day, 0, 0, 0, 0);


这篇关于Cultureinfo和error =&quot;字符串未被识别为有效日期时间“在Windows 10和Windows 7之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆