gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:20:24 24 4
gpt4 key购买 nike

我有一个带有 nulluble createdDate 字段的模型。我想从创建的日期字段中提取月份。这是我在 Controller 方法中的代码。我认为问题出在 LINQ 查询中。你能帮我更正那个查询吗?现在该查询的结果为空。

 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(函数(){ $('.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);
}

})

最佳答案

DateTime.Month method returns an integer that is 1-based: January is equal to 1, and December is equal to 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());

关于javascript - 如何从日期时间字段中提取月份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53497907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com