Anand Gupta
Anand Gupta

Reputation: 183

Date Time from Django views to be used in Javascript

I have this view in views.py:

def endtime(request, *args, **kwargs):
    end_time = datetime.datetime.now() + timedelta(hours=10) 
    return HttpResponse(end_time)

The value returned by this view is 2016-01-31 16:25:09.622489

I have a Javascript function that sends an Ajax request to above view and the returned value is recieved by data parameter,

function myfun(){
  $.get('/endtime/',function(data){
        var date = new Date(); 
        var diff = data - date; //not working
  });
}

The value of data is 2016-01-31 16:25:09.622489 and date is Sun Jan 31 2016 06:38:47 GMT+0530 (IST)

How can I convert either data into date format or vice-versa so that I can take the difference between them.

Upvotes: 1

Views: 2065

Answers (1)

noreen_yue
noreen_yue

Reputation: 26

you got data as a string, and convert it to Date object first. by function Date.parse(dateString) or new Date(dateString)

Upvotes: 1

Related Questions