我从Json获取数据,其中也包含日期。现在日期显示在Numerics中。我能够将Date转换为可读格式,但这不是我的要求。我希望日期以mm / dd / yy格式显示。
$.ajax({
url: '@Url.Action("ReqVacancy", "AdvertisementMaintenance")',
type: 'GET',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: { "RequisitionID": id },
success: function (data) {
var result1 = "";
divid.html('');
$.each(data, function (id, result) {
if (result != 0) {
var result1 = "<tr>"
+ '<td><input type="checkbox" name="VacancyId" id="VacancyId" value=' + result.VacancyId + '></td>'
+ "<td>" + result.VacancyId + "</td>"
+ "<td>" + result.PositionId + "</td>"
+ "<td>" + result.Reason + "</td>"
+ "<td>" + eval('new' + result.StartDate.replace(/\//g, ' ')) + "</td>"
+ "<td>" + eval('new' + result.EndDate.replace(/\//g, ' ')) + "</td>"
+ "<tr>"
$('#tbody').append(result1);
}
});
});
结果就在这里......
VacancyId Position Reason StartDate EndDate
1 Fresher Not Avail Sat Mar 07 2015 00:00:00 GMT+0530 (India Standard Time) Tue Mar 17 2015 00:00:00 GMT+0530 (India Standard Time)
我的要求是:
VacancyId Position Reason StartDate EndDate
1 Fresher Not Avail 03/07/2015 03/17/2015
我可以做上面的变量,然后使用getMonth(),getDate()和getFullYear()。但代码很长,我被要求在一行代码中完成。任何帮助都非常感谢。感谢。
答案 0 :(得分:2)
json与你的日期格式无关,你可以在javascript中做这样的事情。
var startDate = new Date(result.StartDate);
var formattedStartDate = startDate.getMonth() + "/" + startDate.getDate() + "/" + startDate.GetYear();
您的整体代码应如下所示。
$.each(data, function (id, result) {
if (result != 0) {
var startDate = eval('new' + result.StartDate.replace(/\//g, ' '));
var formattedStartDate = startDate.getMonth() + "/" + startDate.getDate() + "/" + startDate.GetYear();
var endDate = eval('new' + result.EndDate.replace(/\//g, ' '));
var formattedEndDate = endDate.getMonth() + "/" + endDate.getDate() + "/" + endDate.GetYear();
var result1 = "<tr>"
+ '<td><input type="checkbox" name="VacancyId" id="VacancyId" value=' + result.VacancyId + '></td>'
+ "<td>" + result.VacancyId + "</td>"
+ "<td>" + result.PositionId + "</td>"
+ "<td>" + result.Reason + "</td>"
+ "<td>" + formattedStartDate + "</td>"
+ "<td>" + formattedEndDate + "</td>"
+ "<tr>"
$('#tbody').append(result1);
}
解决方案:2
为日期创建原型:
Date.prototype.toMMDDYYYY = function(){
return this.getMonth() + "/" + this.getDate() + "/" + this.getFullYear()
};
用例:
new Date().toMMDDYYYY();
在你的屏幕上:
$.each(data, function (id, result) {
if (result != 0) {
var result1 = "<tr>"
+ '<td><input type="checkbox" name="VacancyId" id="VacancyId" value=' + result.VacancyId + '></td>'
+ "<td>" + result.VacancyId + "</td>"
+ "<td>" + result.PositionId + "</td>"
+ "<td>" + result.Reason + "</td>"
+ "<td>" + new Date(result.StartDate.replace(/\//g, ' ')).toMMDDYYYY()+ "</td>"
+ "<td>" + new Date(result.EndDate.replace(/\//g, ' ')).toMMDDYYYY()+ "</td>"
+ "<tr>"
$('#tbody').append(result1);
}
更新:使用实时示例
//Somewhere globally...
Date.prototype.toMMDDYYYY = function(){
return this.getMonth() + "/" + this.getDate() + "/" + this.getFullYear()
};
//expecing your date string looks like this
var startDate = new Date("Sat Mar 07 2015 00:00:00 GMT+0530 (India Standard Time)");
var data = [{"PositionId":1,"Reason":"Fresher","StartDate":"/Date(1425666600000)/","EndDate":"/Date(1426530600000)/","VacancyId":1}];
$.each(data, function (id, result) {
if (result != 0) {
//Your rest of the code
alert( eval("new " + result.StartDate.replace(/\//g, ' ')).toMMDDYYYY());
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
答案 1 :(得分:1)
你可以这样做
$.each(data, function (id, result) {
if (result != 0) {
var stDate=new Date(result.StartDate);
var endDate=new Date(result.EndDate);
var result1 = "<tr>"
+ '<td><input type="checkbox" name="VacancyId" id="VacancyId" value=' + result.VacancyId + '></td>'
+ "<td>" + result.VacancyId + "</td>"
+ "<td>" + result.PositionId + "</td>"
+ "<td>" + result.Reason + "</td>"
+ "<td>" + stDate.getMonth()+1 +"/" + stDate.getDate() + "/" + stDate.getFullYear() + "</td>"
+ "<td>" + endDate.getMonth()+1 +"/" + endDate.getDate() + "/" + endDate.getFullYear() + "</td>"
+ "<tr>"
$('#tbody').append(result1);
}
});
答案 2 :(得分:1)
我刚从@Moksh Shah&amp;获得答案还是@Guruprasad Rao。谢谢你的帮助。它看起来像这样..
$.each(data, function (id, result) {
console.log(result.StartDate);
if (result != 0) {
//I just had to use new Date(......) here
var endDate = new Date(eval('new' + result.EndDate.replace(/\//g, ' ')));
var formattedEDate = endDate.getMonth() + 1 + '/' + endDate.getDate() + '/' + endDate.getFullYear();
var SDate = new Date(eval('new' + result.StartDate.replace(/\//g, ' ')));
var formattedSDate = SDate.getMonth() + 1 + '/' + SDate.getDate() + '/' + SDate.getFullYear();
//console.log(endDate);
var result1 = "<tr>"
+ '<td><input type="checkbox" name="VacancyId" id="VacancyId" value=' + result.VacancyId + '></td>'
+ "<td>" + result.VacancyId + "</td>"
+ "<td>" + result.PositionId + "</td>"
+ "<td>" + result.Reason + "</td>"
+ "<td>" + formattedSDate + "</td>"
+ "<td>" + formattedEDate + "</td>"
+ "<tr>"
$('#tbody').append(result1);
}
答案 3 :(得分:0)
它可能是this
的副本您的代码应如下所示:
if (result != 0) {
var result1 = "<tr>"
+ '<td><input type="checkbox" name="VacancyId" id="VacancyId" value=' + result.VacancyId + '></td>'
+ "<td>" + result.VacancyId + "</td>"
+ "<td>" + result.PositionId + "</td>"
+ "<td>" + result.Reason + "</td>"
+ "<td>" + dateFormat(result.StartDate) + "</td>"
+ "<td>" + dateFormat(result.EndDate) + "</td>"
+ "<tr>"
$('#tbody').append(result1);
}
function dateFormat(dateObject) {
var d = new Date(dateObject);
var day = d.getDate();
var month = d.getMonth();
var year = d.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
year = year.toString().slice(2);
var date = day + "/" + month + "/" + year;
return date;
}