-3
I only opened that question because all the answers I found create functions or go very long ways to answer it.
The fact is: I need date and time in the Brazilian format:
dd/mm/AAAA HH:MM:SS
-3
I only opened that question because all the answers I found create functions or go very long ways to answer it.
The fact is: I need date and time in the Brazilian format:
dd/mm/AAAA HH:MM:SS
0
So after much searching I found the solution:
var agora = new Date();
agora.toLocaleString("pt-br");
console.log(agora);
// 18/03/2019 16:45:00
0
try this:
function now(){
let date = new Date();
let hour = document.querySelector('#hour'); // select the element for print (DOM)
hour.textContent = data.toLocaleTimeString('pt-BR');
}
let autorefresh = setInterval(now, 1000);
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
"Very long functions and paths" exist for several reasons. The main thing is that the native Javascript date API it was never very good. And the method
toLocaleString()
can return another format if the user does not have the correct locale installed on his machine (in this case he will use the locale default system). And there’s the case - rarest nowadays, but it can still happen - of the format relative to the locale change (after all, it’s an approximation of what is used in day-to-day life, and nothing prevents the setting from changing).– hkotsubo
I just wanted to clarify a few points regarding the use of "long functions and paths" (which is not something "bad" or done for nothing) and at no point did I imply that your question is not useful. I’m sorry if I gave you the wrong impression. I just thought it was important to comment on the points regarding
toLocaleString
(whose solution you posted is not wrong, it’s just a point of attention - it’s not 100% guaranteed that at all times will return the same format, for reasons already explained in the comment above). If you want, you can edit your reply by adding this information, so it becomes even more useful.– hkotsubo
Another point is that they already exist several questions about formatting dates in Javascript, such as that one and that one. If you didn’t find an answer exactly the same as yours, it would be better to answer one of the already existing questions rather than duplicate the question (and this would be much more useful, as one of them is one of the first results on Google to "format Javascript dates"). There is no pq to create another question for each minimum format variation, unless it requires a very different solution than has already been proposed.
– hkotsubo
And your answer is basically "use this code," without any explanation, and answers like that are less useful than those that explain things. And it doesn’t have to be a book, any minimal explanation already makes the answer more useful than it is today. For example, why the parameter should be
'pt-br'
? Can I pass any string? Do you have limitations/special cases? etc. It is no use to be clear and direct if the answer does not explain anything, the best answers (the most useful) are precisely those that explain why the code works, the concepts behind it, etc.– hkotsubo
Instead of getting perplexed, why not try to better understand how the site works? See [Ask], [Answer] and the FAQ. Understand that only utility is not enough, because there is a well-defined scope on what can and what can’t be asked. Of course, everyone has their own criteria about what is a good question, but the scope and all the links I’ve gone through exist to try to standardize as much as possible, in order to minimize divergences.
– hkotsubo
Of course, the voting system is not perfect and strange and "unfair" cases can occur, but by and large, I’d say it works fine. Finally, if you disagree with something and want to take the discussion further, you can open a topic in [goal], because there it serves to discuss precisely this type of thing.
– hkotsubo