Django Ajax response date and time format
<p>Got a problem... </p>

<p>I use <code>Django</code>, <code>SQLite</code>, <code>jquery</code> and <code>AJAX</code>.</p>

<p>The problem is that when I get date and time from my database, it looks weird.</p>

<p>Is there any way to display it normally, as <code>dd/mm/yyyy HH:MM</code> ?</p>

<p><strong>Modeles.py</strong></p>

<pre><code>class QueryHistory(models.Model) :
 userID = models.CharField(max_length=100)
 date = models.DateTimeField(auto_now_add=True, blank=True)
</code></pre>

<p><strong>View.py</strong></p>

<pre><code>queries_list = serializers.serialize(
             'json',
             (QueryHistory.objects.filter(
                 userID = request.session['user_id']
               ).order_by('-id')[:5])
           )
return HttpResponse(json.dumps(queries_list), content_type="application/json")
</code></pre>

<p><strong>Js.js</strong></p>

<pre><code>success : function(response) {
 var queries_list = jQuery.parseJSON(response);
 console.log(response);
}
</code></pre>

<blockquote>
  <p><strong>Result:</strong> 2016-09-21T06:43:26.693Z</p>
  
  <p><strong>Should be:</strong> 21/09/2016 06:43</p>
</blockquote>

<p>I came up with my own solution. </p>

<p>to display date in your own format you just need to create a variable <code>new Date</code></p>

<pre><code>var date = new Date();
</code></pre>

<p>after that we parse date from our response to this variable, so change it to:</p>

<pre><code>var date = new Date(ourAjaxResponse.date);
</code></pre>

<p>now if you try to display it, you will get a standard JavaScript date format:</p>

<blockquote>
  <p>Wed Mar 25 2015 13:00:00 GMT+1300 (New Zealand ())</p>
</blockquote>

<p>Thanks to JavaScript, it has a couple of methods that allow us to get Hours, Minutes and everything else we need to display date as we wish.</p>

<pre><code>var date = new Date(ourAjaxResponse.date);
   var day  = date.getDate();
   var mnth = date.getMonth(); 
   var year = date.getFullYear();
   var hrs  = date.getHours();
   var mnts = date.getMinutes();
</code></pre>

<p><code>var mnths</code> will be number from 1 to 12, so you can create an array with list of months in your own language</p>

<pre><code>var monthNames = [
  "Ð¯Ð½Ð²Ð°ÑÑ", "Ð¤ÐµÐ²ÑÐ°Ð»Ñ", "ÐÐ°ÑÑÐ°",
  "ÐÐ¿ÑÐµÐ»Ñ", "ÐÐ°Ñ", "ÐÑÐ½Ñ", "ÐÑÐ»Ñ",
  "ÐÐ²Ð³ÑÑÑÐ°", "Ð¡ÐµÐ½ÑÑÐ±ÑÑ", "ÐÐºÑÑÐ±ÑÑ",
  "ÐÐ¾ÑÐ±ÑÑ", "ÐÐµÐºÐ°Ð±ÑÑ"
]; // For Example: in Russian 

var monthNames = [
  "January", "February", "March",
  "April", "May", "June", "July",
  "August", "September", "October",
  "November", "December"
]; //or in English
</code></pre>

<p><strong>Final result:</strong> </p>

<p>I have function that gets date variable:</p>

<pre><code>function get_date(date){
    var date = new Date(date);
    var day = date.getDate();
    var monthIndex = date.getMonth();
    var year = date.getFullYear();
    var hours = addZero(date.getHours());//addZero() function described below
    var minutes = addZero(date.getMinutes());

    return "&lt;i class='fa fa-calendar-o' aria-hidden='true'&gt;&lt;/i&gt; " + day + " " + monthNames[monthIndex] + " " + year + " &lt;i class='fa fa-clock-o' aria-hidden='true'&gt;&lt;/i&gt; " + hours + ":" + minutes;
}
</code></pre>

<p>Another small function to add zero in front of hours and minutes, because if these are less than 10, the result will be </p>

<blockquote>
  <p>8:21 or 19:8 instead of 08:21 or 19:08 </p>
</blockquote>

<pre><code>function addZero(i) {
    if (i &lt; 10) {
        i = "0" + i;
    }

    return i;
}
</code></pre>

<p>Now, wherever you want in your code you can call this function and parse date to it from your server or any other sources, and you will always get the same result:</p>

<pre><code>console.log(get_date(ourAjaxResponse.date));
</code></pre>

<blockquote>
  <p>22 September 2016  01:16</p>
</blockquote>

<p>if you change <code>return</code> in function <code>get_date()</code> on:</p>

<pre><code>return day + "/" + monthIndex + "/" + year + " " + hours + ":" + minutes;
</code></pre>

<blockquote>
  <p>22/09/2016 01:16</p>
</blockquote>

jquery python ajax django response