In the view, I need to modify only the time part for a field which is of type datetime. I am not sure how this would work. One way may be to pass a hint but not sure how this can be accomplished.
THanks
In the view, I need to modify only the time part for a field which is of type datetime. I am not sure how this would work. One way may be to pass a hint but not sure how this can be accomplished.
THanks
Your question isn't very clear on what you want to do but from what I gather I assume you want to display the date and edit only the time. In this case that can be accomplished like so:
In your model:
public class FooModel
{
public string Date { get; set; }
public string Time { get; set; }
}
In your controller:
public class FooController
{
[HttpGet]
public ActionResult Foo()
{
var dateTime = DateTime.Now();
var model = new Foo
{
Date = dateTime.ToShortDateString(),
Time = dateTime.ToShortTimeString()
};
return View(model);
}
[HttpPost]
public ActionResult Foo(FooModel model)
{
DateTime updatedDateTime;
var dateTime = string.Format("{0} {1}", model.Date, model.Time);
var isValid = DateTime.TryParse(dateTime, out updatedDateTime);
if (!isValid)
{
ModelState.AddModelError("Time", "Please enter a valid Time.");
return View(model);
}
// process updates
return View("Success");
}
}
In your view:
@model FooModel
@using (Html.BeginForm()) {
@Html.ValidationMessage("Time")
Date: @Html.DisplayFor(m => m.Date) <br />
Time: @Html.EditorFor(m => m.Time)
<p>
<input type="submit" value="Save" />
</p>
}
You can use the ToString
method to change the format. HERE is a listing of all the different format options. For time only, skip to the section titled "The "f" Custom Format Specifier"
Console.WriteLine(TimeFld.ToString("hh:mm:ss.f", ci));
// Displays 07:27:15.0
In your MVC View you would do
@TimeFld.ToString("hh:mm:ss.f", ci);
See DateTime.ToString().
Also, here is a list of standard format options.
Or, you can roll your own.
In Razor:
@Foo.ToString("MMMM dd, yyyy")
would return (for today),
November 08, 2011
In .NET it is not possible to change only the time part of a DateTime
field, because DateTime
is an immutable struct. This is from MSDN, "Working with DateTime Members":
When working with the DateTime structure, be aware that a DateTime type is an immutable value. Therefore, methods such as DateTime.AddDays retrieve a new DateTime value instead of incrementing an existing value. The following example illustrates how to increment a DateTime type by a day using the statement dt = dt.AddDays(1).
public static void Main()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
DateTime dt = DateTime.Now;
Console.WriteLine("Today is {0}", DateTime.Now.ToString("d"));
// Increments dt by one day.
dt = dt.AddDays(1);
Console.WriteLine("Tomorrow is {0}", dt.ToString("d"));
}
As you can see, the only valid approach is to assign a new value to the DateTime
field. The DateTime
type does support the time-manipulation methods AddHours
, AddMinutes
, AddSeconds
and AddMilliseconds
. However, these methods only return a new DateTime
value, so you will have to set the property on your View Model to the new value for it to appear in your view.
You may also want to check out this post: How to change time in datetime? . (Jon Skeet answered it).