DateTime.ParseExact:“String not recognized as a valid DateTime” [英] DateTime.ParseExact: "String not recognised as a valid DateTime"
问题描述
我正在尝试从JavaScript应用程序发送JSON请求,并在我的SQL DB中创建此对象(HourRegistration),但由于某种原因,我无法将json的日期解析为有效的DateTime。 / p>
JS的JSON日期
hourRegistration .Date =12/08/2015;
所以 dd / MM / yyyy
p>
我正在尝试解析:
Date = DateTime。 ParseExact(hourRegistration.Date,dd / MM / yyyy,CultureInfo.InvariantCulture)
整个方法
public void CreateHours(HourRegistrationDTO hourRegistration)
{
DAO.Instance。 HourRegistration.Add(new hourRegistration()
{
Date = DateTime.ParseExact(hourRegistration.Date,dd / MM / yyyy,CultureInfo.InvariantCulture),
Cust_ID = hourRegistration.Cust_id,
Login_ID = hourRegistration.Login_id,
Hours = hourRegistration.Hours,
注释= hourRegistration.Comment
});
DAO.Instance.SaveChanges();
}
截图
ToCharArray()
我不知道为什么这不行。据我所知,这应该是有效的?
你的字符串值在 hourRegistration.Date
不是你以为是这样的。
使用.NET中的字符串,有许多Unicode字符在调试表现中根本不出现,或者实际上不是他们似乎是人物。在您的情况下,如 ToCharArray
调试扩展所示,您的字符串实际上包含一些 U + 200E
Unicode字符LEFT-TO-RIGHT MARK(U + 200E)字符。
这些在调试表示中不可见,但在尝试解析日期时,它们是相关的。我不知道他们到达那里或为什么他们在那里 - 这是你需要进一步研究的东西。
要解决您的直接问题,解析日期之前,请从字符串中删除所有非ASCII字符,方式如下:
var actualDateString = new String(hourRegistration.Date
.ToCharArray()
.Where(c => c< = 255)
.ToArray()
);
(我刚刚敲响了它,所以它不是很漂亮)
然后,您应该可以根据需要解析 actualDateString
。
I'm trying to send a JSON request from a javascript application, and create this object (HourRegistration) in my SQL DB, but for some reason, I can't parse the date from json, to a valid DateTime.
JSON date from JS
hourRegistration.Date = "12/08/2015";
So dd/MM/yyyy
I'm trying to parse like so:
Date = DateTime.ParseExact(hourRegistration.Date, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Entire method
public void CreateHours(HourRegistrationDTO hourRegistration)
{
DAO.Instance.HourRegistration.Add(new HourRegistration()
{
Date = DateTime.ParseExact(hourRegistration.Date, "dd/MM/yyyy", CultureInfo.InvariantCulture),
Cust_ID = hourRegistration.Cust_id,
Login_ID = hourRegistration.Login_id,
Hours = hourRegistration.Hours,
Comment = hourRegistration.Comment
});
DAO.Instance.SaveChanges();
}
Screenshot
ToCharArray()
I don't really know why this doesn't work. As far as I recall, this should work?
Your string value in hourRegistration.Date
is not what you think it is.
With strings in .NET, there are many Unicode characters which either do not appear at all in the debug representation, or are not in fact the characters they appear to be. In your case, as shown by the ToCharArray
debug expansion, your string actually contains a number of U+200E
Unicode Character 'LEFT-TO-RIGHT MARK' (U+200E) characters.
These are invisible in the debug representation, but ARE relevant when trying to parse the date. I don't know how they got there or why they are there - that's something you'd need to research further.
To solve your immediate issue, before parsing your date, remove all non-ASCII characters from your string, along the lines of:
var actualDateString = new String(hourRegistration.Date
.ToCharArray()
.Where(c => c <= 255)
.ToArray()
);
(I've just banged this out so it's not very pretty)
Then you should be able to parse actualDateString
as required.
这篇关于DateTime.ParseExact:“String not recognized as a valid DateTime”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!