如何从日期时间字段中提取月份?

时间:2018-11-27 10:45:19

标签: javascript asp.net-mvc linq

我有一个带有nulld createdDate字段的模型。我想从创建的日期字段中提取月份。这是我在控制器方法中的代码。我认为问题出在LINQ查询中。您能帮我纠正该查询吗?现在,该查询的reslts为空。

 public JsonResult GetDispatchNoteByMonth(string month)
        {
            IEnumerable<Domain.DispatchNote.DispatchNote> dispatchNotes = _dispatchNoteService.GetAllDispatchNote().Where(d => d.Status == "Open" && d.CreatedDate.GetValueOrDefault().Month.ToString().Contains(month));
            IEnumerable<DispatchNoteViewModel> models = Mapper.Map<IEnumerable<DispatchNoteViewModel>>(dispatchNotes);

            if(models.Count() > 0)
            {
                return Json(new { IsDataAvailable = true, DispatchNotes = models }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { IsDataAvailable = false, Message = "No Dispatch notes found" }, JsonRequestBehavior.AllowGet);
            }
        }

我通过选择下拉值来传递月份值。这是我的Javascript代码,

$(“#monthDropdown”)。change(function(){     $('。tbtr')。nextAll('tr')。remove();     $('#openDispatchNoteTable .tbtr')。after('');     // $('#spinnerContainer')。jmspinner();

var selectedValue = $("#monthDropdown").val();

if(selectedValue != ""){
    $.ajax({
        url: "/Dispatch/GetDispatchNoteByMonth?month=" + selectedValue,
        cache: false,
        type: "GET",
        success: function (response) {
            $('#openDispatchNoteTable').find("tr:not(:first)").remove();
            $('#spinnerContainer').jmspinner(false);
            if (response.IsDataAvailable) {
                $('.tbtr').nextAll('tr').remove();
                for (var i = 0 ; i <= response.DispatchNotes.length ; i++){
                    if (response.DispatchNotes[i].CreatedDate != null) {
                        $('.tbtr').nextAll('tr').remove();
                        $('#openDispatchNoteTable .tbtr').after('<tr><td> <input type="checkbox" class="dispatchNotesToInvoiceCb" name="dispatchNotesToInvoice" value="' + response.DispatchNotes[i].DispatchNoteId + '" /></td><td>' + response.DispatchNotes[i].DispatchId + '</td><td>' + response.DispatchNotes[i].Client + '</td><td>' + response.DispatchNotes[i].CompanyAddress + '</td><td>' + response.DispatchNotes[i].Quantity + '</td><td>' + response.DispatchNotes[i].Driver + '</td><td>' + response.DispatchNotes[i].VehicleLicensePlateNumber + '</td><td>' + moment(new Date(parseInt(response.DispatchNotes[i].DispatchDate.substr(6))).toLocaleDateString()).format('YYYY-MMMM-DD') + '</td><td>' + response.DispatchNotes[i].CreatedUser + '</td><td>' + moment(new Date(parseInt(response.DispatchNotes[i].CreatedDate.substr(6))).toLocaleDateString()).format('YYYY-MMMM-DD') + '</td><td><a class="editbtn btn btn-default btn-raised" href="/Dispatch/ClosedCancelledView/' + response.DispatchNotes[i].DispatchNoteId + '">View<div class="ripple-container"></div></a></td></tr>');
                    } else {
                        $('.tbtr').nextAll('tr').remove();
                        $('#openDispatchNoteTable .tbtr').after('<tr><td> <input type="checkbox" class="dispatchNotesToInvoiceCb" name="dispatchNotesToInvoice" value="' + response.DispatchNotes[i].DispatchNoteId + '" /></td><td>' + response.DispatchNotes[i].DispatchId + '</td><td>' + response.DispatchNotes[i].Client + '</td><td>' + response.DispatchNotes[i].CompanyAddress + '</td><td>' + response.DispatchNotes[i].Quantity + '</td><td>' + response.DispatchNotes[i].Driver + '</td><td>' + response.DispatchNotes[i].VehicleLicensePlateNumber + '</td><td>' + moment(new Date(parseInt(response.DispatchNotes[i].DispatchDate.substr(6))).toLocaleDateString()).format('YYYY-MMMM-DD') + '</td><td>' + response.DispatchNotes[i].CreatedUser + '</td><td></td><td><a class="editbtn btn btn-default btn-raised" href="/Dispatch/ClosedCancelledView/' + response.DispatchNotes[i].DispatchNoteId + '">View<div class="ripple-container"></div></a></td></tr>');
                    }
                }
            } else {
                $('#checkAll').prop('disabled', true);
                $('.tbtr').nextAll('tr').remove();
                $('#openDispatchNoteTable .tbtr').after('<tr><td colspan="11" align="center" style="color:red">' + response.Message + ' ' + $("#monthDropdown option:selected").text() + '</td></tr>');
            }
        },
        error: function (reponse) {
            console.log(reponse);
            $('.tbtr').nextAll('tr').remove();
            $('#spinnerContainer').jmspinner(false);
            $('#openDispatchNoteTable .tbtr').after('<tr><td colspan="11" align="center" style="color:red">Error occured while fetching dispatch notes. Please try again.</td></tr>');
        }
    })
}
else {
    $('.tbtr').nextAll('tr').remove();
    $('#spinnerContainer').jmspinner(false);
}

})

1 个答案:

答案 0 :(得分:0)

  

DateTime.Month方法返回一个从1开始的整数:   等于1,十二月等于12。

在这种情况下,您应该检查哪个格式的月份变量保持有效。如果您的月份格式为“ MMM”(即“ jan”),则需要转换CreatedDate字段日期

d.CreatedDate.GetValueOrDefault().Month.ToString("MMMM") 




IEnumerable<Domain.DispatchNote.DispatchNote> dispatchNotes = _dispatchNoteService.GetAllDispatchNote().Where(d => d.Status == "Open" && d.CreatedDate.GetValueOrDefault().Month.ToString("MMMM").ToLower().Contains(month).ToLower());