PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] How do you get date time format in an Ajax-call?

 September 08, 2022     ajax, javascript, jquery-ui     No comments   

Issue

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

    });

Solution

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.



Answered By - user1531040
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing