-1

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

DwB
  • 37,124
  • 11
  • 56
  • 82
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • 1
    there really is not enough information in this question for an adequate answer to be formed. What are you trying to do with that datetime? Change its format? Make it editable? What language are you using? MVC is a design pattern not specific to any language or skill set. Post some sample code maybe of what you have vs what you want to have. – benjamin Nov 04 '11 at 23:17
  • I am using c# and I am trying to edit just the Time format. – Nate Pet Nov 07 '11 at 15:24
  • Time in which format you want? Please provide more details – Prasanth Nov 08 '11 at 18:09

4 Answers4

4

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>
}
shuniar
  • 2,592
  • 6
  • 31
  • 38
0

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);
Flexo
  • 87,323
  • 22
  • 191
  • 272
Ryand.Johnson
  • 1,906
  • 2
  • 16
  • 22
0

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

3Dave
  • 28,657
  • 18
  • 88
  • 151
0

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).

Community
  • 1
  • 1
smartcaveman
  • 41,281
  • 29
  • 127
  • 212