simplyvic simplyvic - 4 years ago 169
Javascript Question

Convert JavaScript date() to Python Django models.DateTimeField

I using django model forms to submit data to the database.
I use JavaScript to auto-fill the form with the following

document.getElementById('id_date_received').value = Date();

This outputs:
Mon Feb 06 2017 11:39:05 GMT+0000 (GMT)

while django's models.DateTimeField expects:
2017-02-06 11:39

How do i convert:
Mon Feb 06 2017 11:39:05 GMT+0000 (GMT)
2017-02-06 11:39


Answer Source

You should consider use Moment.js, it's the easiest javascript library to manipulate dates and timezone formats.

So the code would by something like this:

moment(YOUR_DATE_VARIABLE).format('YYYY-MM-DD HH:mm'); // 2017-02-06 11:39

Hope this help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download