c# - 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
解决方案
只需使用
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();
推荐阅读
- java - 创建一个@Service bean的对象,有可能吗?
- vb.net - strword 可以怎么拆分用过的vbcrlf
- r - 如何在 R / RStudio 中以“开发者模式”安装包?
- javascript - 我正在尝试通过他们的教程设置 Leaflet 地图,但有些东西不起作用,任何人都可以看看,看看为什么?
- java - Spring 安全配置 - addFilterBefore 不适用于 PUT、PATCH 和 DELETE
- php - 关闭浏览器时如何删除用于注销的 codeigniter 会话?
- java - 写入后无法从远程读取 Gremlin 数据
- php - 避免页面重新加载和滚动具有许多提交按钮的页面
- html - 在两列中显示 HTML 代码,实际标签
- python - 在没有 AVX 的 CPU 上运行 TensorFlow 2.0