1

My backend is in Django. I used Django's auto_now_add feature on the model to add the current time when that model was created. For example, I am passing this value to the function: 2019-10-08 09:16:20.666754+00:00. How to convert this in local time in Javascript? I have not coded JS. So the line's a bit blurry for me.

I tried the following method:

function localize_time(date) {
    date = new Date(date);
    date = date.toString();
}

Then I saw another SO post to add "UTC", that's not working either. when I am calling a said function from Django's template, it's showing following error:

Uncaught SyntaxError: missing ) after argument list 

It's on that function.

In Django's template, I am calling the function like this:

<script type="text/javascript">
    localize_time({{ user.created_on | safe}});
</script>

If I don't add safe, then the error is:

Uncaught SyntaxError: Unexpected number

Thanks in advance.

Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
O_o
  • 1,103
  • 11
  • 36
  • Possible duplicate of [Convert UTC date time to local date time](https://stackoverflow.com/questions/6525538/convert-utc-date-time-to-local-date-time) – RamblinRose Oct 10 '19 at 14:41
  • @RamblinRose, No. Read the post entirely before marking it as a duplicate. – O_o Oct 10 '19 at 14:53
  • Hmm...I removed the duplicate flag right after I created it, I don't understand why it's here. – RamblinRose Oct 10 '19 at 15:16

2 Answers2

0

I converted the Django's time in milliseconds using datetime.timestamp method and rest of things worked like magic.

O_o
  • 1,103
  • 11
  • 36
0

you need to add UTC at the end of your date string.

const date = new Date('2019-10-08 09:16:20.666754+00:00 UTC');
alert(date.toString())
Ido
  • 51
  • 5