Rohit Rohit - 1 year ago 92
jQuery Question

How to post time using bootstrap datetimepicker from view to controller

I am using bootstrap datetimepicker for date time functionality. While I am trying to post only 'Time' to controller from view the time is coming as '00:00:00' and not the actual selected the time is coming.

Below is my code

public TimeSpan StartTime { get; set; }

public TimeSpan EndTime { get; set; }


@Html.TextBoxFor(model => model.StartTime, new { @class = "form-control", @id = "txtStartTime",@name= "StartTime" })
@Html.TextBoxFor(model => model.EndTime, new { @class = "form-control", @id = "txtEndTime", @name = "EndTime" })

Code written in
function to show only 'Time' from datetimepicker

$('#txtStartTime').datetimepicker({ format: 'LT A' });
$('#txtEndTime').datetimepicker({ format: 'LT A' });

However, on form submit I am getting the value as '00:00:00' only, attached is the screenshot

Any help on this appreciated !

Answer Source

Beacuse you are using the wrong format

$('#txtStartTime').datetimepicker({ format: 'hh:mm:ss'  });
$('#txtEndTime').datetimepicker({ format: 'hh:mm:ss'  });

If you want to use your format make StartTime and EndDate strings in your model and then parse them in Controller.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download