簡體   English   中英

使用 AJAX 將日期時間轉換為字符串

[英]convert datetime to string using AJAX

伙計們,我想尋求您的幫助,我這里有一個使用 AJAX 的獲取方法,我成功地從數據庫中獲取了日期,這是 output 2020-08-13T00:00:00 但是我想將日期格式轉換為 2020-08 -13 如何轉換日期時間? 下面是我的代碼

/獲取/ $(文檔).ready(函數 () {

$.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);
    }
});

})

謝謝您的幫助

這是您可以使用的方法

var dateVal = data[i].StartDate; var dateString = dateVal.substr(0,9);

您將從字符串中獲取前 10 個字符,即日期值。

你可以使用這個 function

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

應該是這樣

$.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);
    }
});

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM