Home > Mobile >  How do you get date time format in an Ajax-call?
How do you get date time format in an Ajax-call?

Time:07-09

I will create a Excel with the "myOrder" and a suffix of the current date. How do you get a filename with a format of "yyyyMMddhhmmss" By example: "20220704073533"?

   $.ajax({
        url: kendo.format('@(Server.UrlDecode(Url.Action("myExcel", "NewOrder", new { orderKeys = "{0}" })))', ids.join()),
        method: 'POST',
        xhrFields: {
            responseType: 'blob'
        },
        success: function (data) {
            var a = document.createElement('a');
            var url = window.URL.createObjectURL(data);
            a.href = url;
            a.download = 'myOrder'   date().format('yyyyMMddhhmmss')   '.xlsx';
            document.body.append(a);
            a.click();
            a.remove();
            window.URL.revokeObjectURL(url);
        }

    });

CodePudding user response:

I have written a function like this

function FormatDateTime() {

    var dateObj = new Date();

    var month = (dateObj.getUTCMonth()   1).toString().padStart(2, "0");      
    var day = dateObj.getUTCDate().toString().padStart(2, "0");
    var year = dateObj.getUTCFullYear().toString().padStart(4, "0");
    var hours = dateObj.getUTCHours().toString().padStart(2, "0");
    var minutes = dateObj.getUTCMinutes().toString().padStart(2, "0");
    var seconds = dateObj.getUTCSeconds().toString().padStart(2, "0");

    var dateStr = year   month   day   hours   minutes   seconds;

    return dateStr;
}

And then...

<script src="~/Scripts/FormatDateTime.js"></script>

var dateStr = FormatDateTime();

And in the Ajax-call:

 a.download = 'myOrder'   dateStr   '.xlsx';

And that works for me.

  • Related