Skip to content
Advertisement

convert datetime to string using AJAX

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 () {

$.ajax({
    url: 'URL',
    dataType: 'json',
    success: function (data) {           
        console.log(data);
        console.log(data.length);
        var badgeType = '';
        var overdueCount = 0;
        var inprogressCount = 0;
        var newCount = 0;
        var notstartedCount = 0;
        var completedCount = 0;
        for (var i = 0; i < data.length; i++) {
            switch (data[i].Progress) {
                case 'Overdue':
                    badgeType = 'badge-danger';
                    overdueCount += 1;                        
                    break;
                case 'In Progress':
                    badgeType = 'badge-success';
                    inprogressCount += 1;
                    break;
                case 'New':
                    badgeType = 'badge-warning';
                    newCount += 1;
                    break;
                case 'Not Started':
                    badgeType = 'badge-info';
                    notstartedCount += 1;
                    break;
                case 'Completed':
                    badgeType = 'badge-secondary';
                    completedCount += 1;
                    break;
            }
            var row = $(
                '<tr class="table-row" data-toggle="modal" data-target="#editTask" onclick="editModal(this)">' +
                '<td style="display:none;">' + data[i].TaskId + '</td>' +
                '<td>' + data[i].Priority + '</td>' +
                '<td>' + data[i].TaskName + '</td>' +
                '<td><h6 class="h6 mb-2"><span class="badge badge-sm ' + badgeType + '">' + data[i].Progress + '</span></h6></td>' +
                '<td style="display:none;">' + data[i].StartDate + '</td>' +
                '<td>' + data[i].EndDate + '</td>' +
                '<td style="display:none;">' + data[i].Comments + '</td>' +
                '</tr>'
            );                
            $('#tbodyId').append(row);                
        }
        $('#overdueCount').append('<h1>' + overdueCount + '</h1>');
        $('#inprogressCount').append('<h1>' + inprogressCount + '</h1>');
        $('#newCount').append('<h1>' + newCount + '</h1>');
        $('#notstartedCount').append('<h1>' + notstartedCount + '</h1>');
        $('#completedCount').append('<h1>' + completedCount + '</h1>');

        
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert('Error: ' + textStatus + ' - ' + errorThrown);
    }
});

})

Thank you for your help

Advertisement

Answer

you can use this function

const dateFormat = (dateFromDB) => {
   var dateValue = new Date(dateFromDB);
   return dateValue.getFullYear()+'-'+('00'+(dateValue.getMonth()+1)).slice(-2)+'-'+dateValue.getDate()
}

it should be this way

$.ajax({
    url: 'URL',
    dataType: 'json',
    success: function (data) {           
        console.log(data);
        console.log(data.length);
        var badgeType = '';
        var overdueCount = 0;
        var inprogressCount = 0;
        var newCount = 0;
        var notstartedCount = 0;
        var completedCount = 0;
        for (var i = 0; i < data.length; i++) {
            switch (data[i].Progress) {
                case 'Overdue':
                    badgeType = 'badge-danger';
                    overdueCount += 1;                        
                    break;
                case 'In Progress':
                    badgeType = 'badge-success';
                    inprogressCount += 1;
                    break;
                case 'New':
                    badgeType = 'badge-warning';
                    newCount += 1;
                    break;
                case 'Not Started':
                    badgeType = 'badge-info';
                    notstartedCount += 1;
                    break;
                case 'Completed':
                    badgeType = 'badge-secondary';
                    completedCount += 1;
                    break;
            }
            var row = $(
                '<tr class="table-row" data-toggle="modal" data-target="#editTask" onclick="editModal(this)">' +
                '<td style="display:none;">' + data[i].TaskId + '</td>' +
                '<td>' + data[i].Priority + '</td>' +
                '<td>' + data[i].TaskName + '</td>' +
                '<td><h6 class="h6 mb-2"><span class="badge badge-sm ' + badgeType + '">' + data[i].Progress + '</span></h6></td>' +
                '<td style="display:none;">' + dateFormat(data[i].StartDate) + '</td>' +
                '<td>' + dateFormat(data[i].EndDate) + '</td>' +
                '<td style="display:none;">' + data[i].Comments + '</td>' +
                '</tr>'
            );                
            $('#tbodyId').append(row);                
        }
        $('#overdueCount').append('<h1>' + overdueCount + '</h1>');
        $('#inprogressCount').append('<h1>' + inprogressCount + '</h1>');
        $('#newCount').append('<h1>' + newCount + '</h1>');
        $('#notstartedCount').append('<h1>' + notstartedCount + '</h1>');
        $('#completedCount').append('<h1>' + completedCount + '</h1>');

        
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert('Error: ' + textStatus + ' - ' + errorThrown);
    }
});

enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement