NET MVC使用Jquery向时间的字符串表示形式中添加小时

NET MVC使用Jquery向时间的字符串表示形式中添加小时,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我有两个下拉列表,开始时间和结束时间,以30分钟的间隔填充时间,以及 当用户选择新的开始时间时,我试图将EndTime设置为比StartTime晚一小时 我的模型: model.Times = new[] { new SelectListItem{ Text="12:00 AM", Value = "12:00 AM" }, new SelectListItem{ Text="12:30 AM", Value = "12:30 AM" }, new SelectListItem{ Text="1

我有两个下拉列表,开始时间和结束时间,以30分钟的间隔填充时间,以及 当用户选择新的开始时间时,我试图将EndTime设置为比StartTime晚一小时

我的模型:

model.Times = new[]
{
new SelectListItem{ Text="12:00 AM", Value = "12:00 AM" },
new SelectListItem{ Text="12:30 AM", Value = "12:30 AM" },
new SelectListItem{ Text="1:00 AM", Value = "1:00 AM" },
etc
}
我的cshtml:

@Html.DropDownListFor(x => x.StartTime, Model.Times, new {  @id = "ddlStartTime"})
@Html.DropDownListFor(x => x.EndTime, Model.Times, new {  @id = "ddlEndTime"})
我可以通过以下方式将EndTime dropdownlist设置为等于StartTime dropdownlist:

$("#ddlStartTime").change(function () {                
    var selectedValue = $(this).val();
    $('#EndTime').val(newDateObj);
});
但是如何将结束时间设置为一小时后?(同时处理开始时间为晚上11:00和11:30的事件)


感谢您的帮助

使用此JQuery添加小时


这可能不是最干净的代码,但它就是这样。您可以使用javascript
Date
来帮助您转换24小时左右的时间

var selectedValue = "1:00 AM";
var hours, minutes, endDate, endDateString, ampm;

hours = selectedValue.split(':')[1].substring(3, 5) == "AM" ? parseInt(selectedValue.substring(0, 2)) : parseInt(selectedValue.substring(0, 2)) + 12;
minutes = parseInt(selectedValue.split(':')[1].substring(0, 2));
endDate = new Date(0, 0, 0, hours + 1, minutes, 0, 0);

if (endDate.getHours() >= 12) {
    ampm = "PM";
    hours = endDate.getHours() - 12;
}
else {
    ampm = "AM";
    hours = endDate.getHours();
}
if (hours === 0) {
    endDateString = 12;
}
else {
    endDateString = hours < 10 ? '0' + hours.toString() : hours.toString();
}
endDateString += endDate.getMinutes() < 10 ? ':0' + endDate.getMinutes() + ' ' + ampm : ':' + endDate.getMinutes() + ' ' + ampm;
var selectedValue=“凌晨1:00”;
变量小时、分钟、结束日期、结束日期字符串、ampm;
小时数=所选值。拆分(“:”)[1]。子字符串(3,5)=“AM”?parseInt(selectedValue.substring(0,2)):parseInt(selectedValue.substring(0,2))+12;
分钟数=parseInt(selectedValue.split(':')[1]。子字符串(0,2));
endDate=新日期(0,0,0,小时+1,分钟,0,0);
如果(endDate.getHours()>=12){
ampm=“PM”;
小时数=endDate.getHours()-12;
}
否则{
ampm=“AM”;
小时数=endDate.getHours();
}
如果(小时==0){
endDateString=12;
}
否则{
endDateString=hours<10?'0'+hours.toString():hours.toString();
}
endDateString+=endDate.getMinutes()<10?':0'+endDate.getMinutes()+''+ampm:':'+endDate.getMinutes()+''+ampm;
关于
.split
,代码有点粗糙,但您应该了解如何进行拆分,并对代码进行自己的微调


您可以在中找到用于测试目的的小提琴。

很好的答案,除了不能处理晚上11:30(将其改为中午12:30,而不是上午12:30)之外,效果很好,但我可以用if处理。
var selectedValue = "1:00 AM";
var hours, minutes, endDate, endDateString, ampm;

hours = selectedValue.split(':')[1].substring(3, 5) == "AM" ? parseInt(selectedValue.substring(0, 2)) : parseInt(selectedValue.substring(0, 2)) + 12;
minutes = parseInt(selectedValue.split(':')[1].substring(0, 2));
endDate = new Date(0, 0, 0, hours + 1, minutes, 0, 0);

if (endDate.getHours() >= 12) {
    ampm = "PM";
    hours = endDate.getHours() - 12;
}
else {
    ampm = "AM";
    hours = endDate.getHours();
}
if (hours === 0) {
    endDateString = 12;
}
else {
    endDateString = hours < 10 ? '0' + hours.toString() : hours.toString();
}
endDateString += endDate.getMinutes() < 10 ? ':0' + endDate.getMinutes() + ' ' + ampm : ':' + endDate.getMinutes() + ' ' + ampm;