gpt4 book ai didi

c# - Asp.net mvc3、C#、Datetime.Parse()

转载 作者:行者123 更新时间:2023-12-02 05:38:13 25 4
gpt4 key购买 nike

这很奇怪。我有来自 Http GET 的日期格式数据,我使用模型类包装日期。是这样的,

// Model

public class wrapModel
{
public DateTime mydate{get; set;}
}

还有,

// controller
[HttpGet]
public void myController(wrapModel data){
Response.Write(searchModel.mydate.ToString());
}

并使用浏览器调用 Controller ,myhost/home/myController/mydate=05/05/2012

然后它打印 "5/5/2012 12:00:00 AM" ,我预计只有 5/5/2012。

我不需要时间。所以我尝试解析日期。

data.mydata = DateTime.parse(data.mydata.ToString("MM/dd/yyyy"));

但它仍然打印“5/5/2012 12:00:00 AM”

如何将格式更改为“5/5/2012”?

有知道的请指教~

谢谢!

我想解析来自 Http GET 的日期时间格式数据。

但我不改也

[编辑]

感谢您的所有回答和宝贵的时间!但我仍然有问题。请多帮帮我 :)

首先,我需要 DateTime 格式的数据,而不是字符串类型,因为我将使用 Linq 中的日期来检索数据库日期。

我试过了,

1)

[DisplayFormat(DataFormatString="{0:MM/dd/yyyy}", ApplyFormatInEditMode=true)]
public DateTime mydate { get; set; }

[HttpGet]
public ActionResult myController(wrapModel data){
return Content(searchModel.mydate.ToString());
}

仍然打印“5/5/2012 12:00:00 AM”

2)

[HttpGet]
public ActionResult myController(wrapModel data){
return Content(searchModel.mydate.ToString("MM/dd/yyyy"));
}

它也更改了格式,但我需要 DateTime 格式,所以我再次转换为 DateTime。

[HttpGet]
public ActionResult myController(wrapModel data){
searchModel.mydate = DateTime.Parse(searchModel.mydate.ToString("MM/dd/yyyy"));
return Content(searchModel.mydate.ToString());
}

然后它再次打印“5/5/2012 12:00:00 AM”。

3)

searchModel.etaDate.ToString("d")

与上面相同,转换为 DateTime 然后打印“5/5/2012 12:00:00 AM”

我使用 Response.Write() 进行简单的返回值测试,但是有什么重要的原因我不能使用 Response.Write() 方法吗?我想学习:)

非常感谢!

[编辑]

为此我需要 DateTime 类型的数据,

Repository myRespository = new Respository();

var data = (from x in myRepository.myContext
where x.thedate == mydate // thedate is datetime type, so I need datetime type for compare
select x.no).SingleOrDefault()

我试过了,

string test = mydate.ToShortDateString();

(...
where x.thedate.ToString() == test
...)

但它不起作用。

最佳答案

How can I change the format to "5/5/2012"?

您首先必须使用正确的格式传递参数,对于 GET 请求,该格式始终 yyyy-MM-dd:

http://myhost/home/myController/mydate=2012-05-05

我强调始终这个词,因为理解这一点非常重要。原因是默认模型绑定(bind)器总是在解析来自 GET 请求的日期时使用 InvariantCulture,并在解析来自 POST 请求的日期时使用当前区域性。有一个 nice article解释这一点以及此设计决策背后的原因。

然后:

Response.Write(searchModel.mydate.ToString("MM/dd/yyyy"));

嗯,实际上,不是Response.Write,因为您永远不会在 ASP.NET MVC 应用程序中这样做,您宁愿从 Controller 操作返回一个 ActionResult:

[HttpGet]
public ActionResult MyAction(wrapModel data)
{
return Content(data.mydate.ToString("MM/dd/yyyy"));
}

或者您将使用 DisplayFormat 属性装饰您的 View 模型:

public class wrapModel
{
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime mydate { get; set; }
}

或者如果您想考虑当前文化短日期格式:

public class wrapModel
{
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime mydate { get; set; }
}

然后让您的 Controller 操作将此模型传递给 View :

public ActionResult MyAction(wrapModel data)
{
return View(data);
}

当然在相应的强类型 View 中使用 DisplayFor/EditorFor 帮助程序以所需格式实现所需输出:

@model wrapModel
<span>
@Html.DisplayFor(x => x.mydate)
</span>

关于c# - Asp.net mvc3、C#、Datetime.Parse(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11381275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com