It is very weird. I hava date format data that comes from Http GET, and I wrap the date using model class. it is like this,
// Model
public class wrapModel
{
public DateTime mydate{get; set;}
}
And,
// controller
[HttpGet]
public void myController(wrapModel data){
Response.Write(searchModel.mydate.ToString());
}
and call the controller using browser, myhost/home/myController/mydate=05/05/2012
then it print “5/5/2012 12:00:00 AM” ,I expected 5/5/2012 only.
I do not need the time. so I tried to parse the date.
data.mydata = DateTime.parse(data.mydata.ToString("MM/dd/yyyy"));
but it still print “5/5/2012 12:00:00 AM”
How can I change the format to “5/5/2012”?
anybody know, please advice me~
Thank you!
I want to parse datetime format data that comes from Http GET.
but I does not changed and also
[EDIT]
Thank you for all the answers and your valuable time! but I am still in the problem. Please help me a little more 🙂
First of all, I need DateTime format data, not string type, becuase I will use the date in Linq to retrieve DB date.
I tried,
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());
}
still it print “5/5/2012 12:00:00 AM”
2)
[HttpGet]
public ActionResult myController(wrapModel data){
return Content(searchModel.mydate.ToString("MM/dd/yyyy"));
}
it changed foramt as well, but I need DateTime format so I convert to DateTime again.
[HttpGet]
public ActionResult myController(wrapModel data){
searchModel.mydate = DateTime.Parse(searchModel.mydate.ToString("MM/dd/yyyy"));
return Content(searchModel.mydate.ToString());
}
then it print “5/5/2012 12:00:00 AM” again.
3)
searchModel.etaDate.ToString("d")
same to above, convert to DateTime then it print “5/5/2012 12:00:00 AM”
And
I use Response.Write() for simple return value test, but is there any important reason I can not use the Response.Write() method? I want to learn 🙂
Thanks a lot!
[EDIT]
I need to DateTime type data for this,
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()
I tried,
string test = mydate.ToShortDateString();
(...
where x.thedate.ToString() == test
...)
but it does not work.
You will first have to pass the parameter using the correct format which is always
yyyy-MM-ddfor GET requests:I am stressing on the word always, because this is very important to understand. The reason for this is that the default model binder always uses InvariantCulture when parsing dates from GET requests and it uses the current culture when parsing dates from POST requests. There’s a nice article explaining this as well as the reasons behind this design decision.
and then:
Well, actually, not
Response.Writebecause you never do that in an ASP.NET MVC application, you’d rather return an ActionResult from a controller action:or you will have your view model decorated with the
DisplayFormatattribute:or if you want to take into account the current culture short date format:
and then have your controller action pass this model to the view:
and of course inside the corresponding strongly typed view use the
DisplayFor/EditorForhelpers to achieve the desired output in the desired format: