调试我的C#windows应用程序datetime转换工作时。但是当它部署时它不起作用 [英] When debug my C# windows application datetime converting works. But when it's deploy it's not working

查看:87
本文介绍了调试我的C#windows应用程序datetime转换工作时。但是当它部署时它不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从XML文件获取到期日期并转换为datetime。它适用于调试模式。但在部署源代码后,它返回错误。



public LabelInfo ParseLabelsingalData(XmlNode Llist)

{

LabelInfo labelinfor = new LabelInfo();



labelinfor.ID = Llist.Attributes [Name]。值;

labelinfor.Name = Llist.Attributes [Name]。价值;



labelinfor.ExpiryDate = Convert.ToDateTime(Llist.Attributes [ExpiryDate]。Value);

}



我从xml获取日期,如31/01/2018



返回错误是 - 字符串未被识别为valide日期时间



我尝试过:



我试图转换日期时间labelinfor.ExpiryDate = Convert.ToDateTime(Llist.Attributes [ExpiryDate]。Value);

解决方案

几乎可以肯定,开发机器上使用的DateTime格式与生产机器上的DateTime格式不同 - Convert.ToDateTime使用当前机器设置作为转换过程的格式。



我强烈建议您忘记所有转换功能,并在将来查看TryParse或TryParseExact。



在这种情况下,我会检查你的XML数据并使用DateTime.TryParseExact来准确指定文件将包含的格式,因此你不能以任何方式依赖于机器设置。

不要使用转换来解决此类问题,请使用 DateTime.TryParse [ ^ ]。这允许更多地控制日期的格式。请记住,日期表示因文化而异;例如在美国日期格式中,31/01/201无效,因为31是月份字段。


I am getting expiry date from XML file and convert to datetime. it's work on debug mode. but after deploy the source code it's return error.

public LabelInfo ParseLabelsingalData(XmlNode Llist)
{
LabelInfo labelinfor = new LabelInfo();

labelinfor.ID = Llist.Attributes["Name"].Value;
labelinfor.Name = Llist.Attributes["Name"].Value;

labelinfor.ExpiryDate = Convert.ToDateTime(Llist.Attributes["ExpiryDate"].Value);
}

I am getting date from xml like "31/01/2018"

return error is - string was not recognized as a valide datetime

What I have tried:

I have tried to convert date time labelinfor.ExpiryDate = Convert.ToDateTime(Llist.Attributes["ExpiryDate"].Value);

解决方案

The problem is almost certainly that the DateTime format used on your development machine is not the same as that on the production machine - and Convert.ToDateTime uses the current machine setting as the format for the conversion process.

I'd strongly recommend that you forget all the Convert functions, and look at TryParse or TryParseExact in future.

In this case, I'd check your XML data and use DateTime.TryParseExact to specify exactly the format the file will contain, so you aren;t reliant in any way on machine settings.


Do not use Convert for such issues, use DateTime.TryParse[^]. This allows more control over the format of the date. Remember that date representations vary according to culture; e.g. in US date format, 31/01/2018 is not valid as 31 is the month field.


这篇关于调试我的C#windows应用程序datetime转换工作时。但是当它部署时它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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