Asp.net mvc 4 将字符串mm/dd/yyyy格式日期更改为datetime dd/mm/yyyy格式日期

Asp.net mvc 4 将字符串mm/dd/yyyy格式日期更改为datetime dd/mm/yyyy格式日期,asp.net-mvc-4,datetimepicker,datetime-format,Asp.net Mvc 4,Datetimepicker,Datetime Format,我在接受控制器中日期的参数时使用了字符串 public ActionResult Export(string fromDate, string toDate) { if (!string.IsNullOrEmpty(fromDates)) DateTime dDate = DateTime.Parse(fromDates); if (!string.IsNullOrEmpty(toDates)) DateTime dDate = DateTim

我在接受控制器中日期的参数时使用了字符串

public ActionResult Export(string fromDate, string toDate)
{
    if (!string.IsNullOrEmpty(fromDates))
         DateTime dDate = DateTime.Parse(fromDates);
    if (!string.IsNullOrEmpty(toDates))
         DateTime dDate = DateTime.Parse(toDates);
}
“2018年3月30日”转换为2018年3月30日凌晨12:00:00


请帮帮我。谢谢。

使用
DateTime.ParseExact
CultureInfo.InvariantCulture

DateTime fromDateDt = DateTime.ParseExact( fromDate, "MM/dd/yyyy", CultureInfo.InvariantCulture );
DateTime toDateDt   = DateTime.ParseExact( toDate  , "MM/dd/yyyy", CultureInfo.InvariantCulture );

使用
dateme.ParseExact
您可以使用以下代码进行尝试:

DateTime ConvertedVal = DateTime.ParseExact("03/30/2018", "MM/dd/yyyy", null);
示例演示

您如何知道输入值的日期格式始终是
MM/dd/yyyy
?它是否取决于客户端计算机的设置?是什么让您认为
DateTime
值具有特定的格式?它们以非人类可读的二进制格式存储,并且仅在转换为字符串后以任何其他方式显示,而字符串是完全不同的类型。现在您可以将aa/bb/yyyy字符串解释(解析)为DateTime,以不同的方式交换day和month的a和b值,但这仍然是一个字符串。一旦将其解析为日期时间,该源字符串值就不相关了。