首页 > 解决方案 > C# Windows 应用程序日期时间在部署时不起作用

问题描述

我从 XML 文件中获取到期日期,然后将其转换为 DateTime。这适用于调试模式。但是,在它被部署后,它会返回一个错误。

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); 
}

我从 xml 中获取日期,如“31/01/2018”返回错误是 - 字符串未被识别为有效日期时间。在这种情况下,我找到了一个解决方案,并尝试使用此函数转换日期时间。

string format = "dd/MM/yyyy";
if (DateTime.TryParseExact(dtime, format, 
System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out 
Temdate))
{
    labelinfor.MinimumExpiryDate = Temdate;
}

因此,当我将此输出值等于对象变量时,它会显示日期和月份交换。

labelinfor.MinimumExpiryDate = Temdate; 
SysLog.WriteLog("Trace ExpiryDate - " + labelinfor.ExpiryDate);-  01/31/2018

标签: c#.netc#-4.0

解决方案


只需使用

Tempdate.ToString(format);

const string format = "dd/MM/yyyy";
//31/01/2018
Console.WriteLine( DateTime.ParseExact("31/01/2018",format,CultureInfo.InvariantCulture,DateTimeStyles.None).ToString(format));
Console.ReadLine();

推荐阅读