javascript – How to show only date without time with Date()

Question:

I'm setting up a date to be displayed in an input, this will be today's date minus one day, but I only need the date and it's showing me Date and Time, how can I do it?

What I have so far is this:

var Hoje = new Date();
Hoje.setDate(Hoje.getDate() - 1);
var Today = Hoje.toLocaleString();
var Today = Today.replace(new RegExp("/", 'g'),"-" );
editors['DataIndice'].setValue(Today);

Answer:

I would do like this:

 var data = new Date().toLocaleString().substr(0, 10) console.log(data)

But it's important to report that when I used this it didn't work very well in Internet Explorer (as might be expected).

In any case, I always recommend using the MomentJS library

Example:

var data = moment().format('DD/MM/YYYY');



console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>

To decrease a day in moment , you can use add method

var date = moment().add(-1, 'days').format('DD/MM/YYYY');


console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.min.js"></script>
Scroll to Top