1

Hi so I've been developing a countdown timer account, and I made a code like the following (following a tutorial):

{% for time in timer %}
   <p id="tm" style="margin-top:-8px;margin-bottom:20px;display:none;">{{time.time|date:"M d, Y H:m:s"}}</p>
{% endfor %}

I have set the timer in the clock widget to 03:50 but when I console.log via javascript

const timeLength = documents.getElementById('tm');
console.log(timeLength.textContent)

It prints 03:12, always been read as month instead of minutes. But the minute is always set to be read as month,
enter image description here

And I am already sure that M (uppercase) supposed to represent month, and the m (lowercase) represents the minute, right?? Where did I go wrong? I have been stuck here for days, and from the research on google I have done, always said that M is month, and m is minute..
Thank you in advance.

ioalft
  • 91
  • 1
  • 5
  • create a date object like `let date = new Date()` in js. you can enter your django templatevalue and format it in the way you want it https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript for further date reference – hansTheFranz Dec 26 '20 at 20:05

1 Answers1

2

And I am already sure that M (uppercase) supposed to represent month, and the m (lowercase) represents the minute, right??

No. The date specifier is similar to PHP's date formatter. As the documentation section on the date format says:

Format character Description Example Output
m Month, 2 digits with leading zeros. '01' to '12'
(…) (…) (…)
M Month, textual, 3 letters. 'Jan'
(…) (…) (…)
i Minutes. '00' to '59'

You thus use i for minutes:

{{ time.time|date:"M d, Y H:i:s" }}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555