在 C# 中格式化日期时出现问题

Issue while formatting date in c#

我将 TextBox DateTime 值传递给 api 中的实体模型,并将日期时间格式化为 CultureInfo("en-GB", true),同时将数据保存到数据库中。

它将日显示为月份,将月份显示为日期。

这是我的代码:

IFormatProvider FormatDate = new System.Globalization.CultureInfo("en-GB", true);

Convert.ToDateTime(user.DOB, FormatDate);

我将 dd-MM-yyyy 格式的日期“08-04-2019”传递到 user.DOB 到 api。

user.DOB 显示日 = 4 和月 = 8 您可以在下图中查看...

你也可以这样做。

var mydate = new DateTime(1976, 10, 13);

我通过

解决了我的问题

在 DTO 中将 public DateTime DOB { get; set; } 转换为 public string DOB { get; set; }

然后转换为 en-GB 格式的日期时间,同时进入保存过程

IFormatProvider FormatDate = new System.Globalization.CultureInfo("en-GB", true);

Convert.ToDateTime(user.DOB, FormatDate);