Guys I would like to ask for your help I have here a Get Method using AJAX I successfully get the date from database this is the output 2020-08-13T00:00:00 however I wanted to convert the date format to this 2020-08-13 how can I convert the the datetime? below is my code
/Get/ $(document).ready(function () {
JavaScript
x
61
61
1
$.ajax({
2
url: 'URL',
3
dataType: 'json',
4
success: function (data) {
5
console.log(data);
6
console.log(data.length);
7
var badgeType = '';
8
var overdueCount = 0;
9
var inprogressCount = 0;
10
var newCount = 0;
11
var notstartedCount = 0;
12
var completedCount = 0;
13
for (var i = 0; i < data.length; i++) {
14
switch (data[i].Progress) {
15
case 'Overdue':
16
badgeType = 'badge-danger';
17
overdueCount += 1;
18
break;
19
case 'In Progress':
20
badgeType = 'badge-success';
21
inprogressCount += 1;
22
break;
23
case 'New':
24
badgeType = 'badge-warning';
25
newCount += 1;
26
break;
27
case 'Not Started':
28
badgeType = 'badge-info';
29
notstartedCount += 1;
30
break;
31
case 'Completed':
32
badgeType = 'badge-secondary';
33
completedCount += 1;
34
break;
35
}
36
var row = $(
37
'<tr class="table-row" data-toggle="modal" data-target="#editTask" onclick="editModal(this)">' +
38
'<td style="display:none;">' + data[i].TaskId + '</td>' +
39
'<td>' + data[i].Priority + '</td>' +
40
'<td>' + data[i].TaskName + '</td>' +
41
'<td><h6 class="h6 mb-2"><span class="badge badge-sm ' + badgeType + '">' + data[i].Progress + '</span></h6></td>' +
42
'<td style="display:none;">' + data[i].StartDate + '</td>' +
43
'<td>' + data[i].EndDate + '</td>' +
44
'<td style="display:none;">' + data[i].Comments + '</td>' +
45
'</tr>'
46
);
47
$('#tbodyId').append(row);
48
}
49
$('#overdueCount').append('<h1>' + overdueCount + '</h1>');
50
$('#inprogressCount').append('<h1>' + inprogressCount + '</h1>');
51
$('#newCount').append('<h1>' + newCount + '</h1>');
52
$('#notstartedCount').append('<h1>' + notstartedCount + '</h1>');
53
$('#completedCount').append('<h1>' + completedCount + '</h1>');
54
55
56
},
57
error: function (jqXHR, textStatus, errorThrown) {
58
alert('Error: ' + textStatus + ' - ' + errorThrown);
59
}
60
});
61
})
Thank you for your help
Advertisement
Answer
you can use this function
JavaScript
1
5
1
const dateFormat = (dateFromDB) => {
2
var dateValue = new Date(dateFromDB);
3
return dateValue.getFullYear()+'-'+('00'+(dateValue.getMonth()+1)).slice(-2)+'-'+dateValue.getDate()
4
}
5
it should be this way
JavaScript
1
61
61
1
$.ajax({
2
url: 'URL',
3
dataType: 'json',
4
success: function (data) {
5
console.log(data);
6
console.log(data.length);
7
var badgeType = '';
8
var overdueCount = 0;
9
var inprogressCount = 0;
10
var newCount = 0;
11
var notstartedCount = 0;
12
var completedCount = 0;
13
for (var i = 0; i < data.length; i++) {
14
switch (data[i].Progress) {
15
case 'Overdue':
16
badgeType = 'badge-danger';
17
overdueCount += 1;
18
break;
19
case 'In Progress':
20
badgeType = 'badge-success';
21
inprogressCount += 1;
22
break;
23
case 'New':
24
badgeType = 'badge-warning';
25
newCount += 1;
26
break;
27
case 'Not Started':
28
badgeType = 'badge-info';
29
notstartedCount += 1;
30
break;
31
case 'Completed':
32
badgeType = 'badge-secondary';
33
completedCount += 1;
34
break;
35
}
36
var row = $(
37
'<tr class="table-row" data-toggle="modal" data-target="#editTask" onclick="editModal(this)">' +
38
'<td style="display:none;">' + data[i].TaskId + '</td>' +
39
'<td>' + data[i].Priority + '</td>' +
40
'<td>' + data[i].TaskName + '</td>' +
41
'<td><h6 class="h6 mb-2"><span class="badge badge-sm ' + badgeType + '">' + data[i].Progress + '</span></h6></td>' +
42
'<td style="display:none;">' + dateFormat(data[i].StartDate) + '</td>' +
43
'<td>' + dateFormat(data[i].EndDate) + '</td>' +
44
'<td style="display:none;">' + data[i].Comments + '</td>' +
45
'</tr>'
46
);
47
$('#tbodyId').append(row);
48
}
49
$('#overdueCount').append('<h1>' + overdueCount + '</h1>');
50
$('#inprogressCount').append('<h1>' + inprogressCount + '</h1>');
51
$('#newCount').append('<h1>' + newCount + '</h1>');
52
$('#notstartedCount').append('<h1>' + notstartedCount + '</h1>');
53
$('#completedCount').append('<h1>' + completedCount + '</h1>');
54
55
56
},
57
error: function (jqXHR, textStatus, errorThrown) {
58
alert('Error: ' + textStatus + ' - ' + errorThrown);
59
}
60
});
61