How to show only the date without the time with Date ()

5

I'm setting a date to be displayed in an input, this will be today's date minus one day, but I need only the date and you're 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);
    
asked by anonymous 24.11.2017 / 12:50

2 answers

5

var Hoje = new Date();
Hoje.setDate(Hoje.getDate() - 1);
//string apenas de data em um formato determinado pelo browser
var Today = Hoje.toDateString();
// string apenas de data no formato localizado do seu sistema
var Today2 = Hoje.toLocaleDateString();

var dataTracinho = Today2.replace(new RegExp("/","g"), "-"); 

console.log (Today);
console.log (Today2);
console.log (dataTracinho);

support:

I tested the top 6 browsers, Chome, IE, Firefox Edge, Opera

andSafari

  

Forallbrowsersyoucanuse Datejs   see one by clicking here

<script type="text/javascript" src="date.js"></script>
<script language="javascript">
   var d1 = Date.parse('today');;
   document.write(d1.toString('dd-MM-yyyy'));
</script>
    
24.11.2017 / 12:56
6

I would do so:

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

 console.log(data)
But it is important to report that when I used this, it did not work very well in Internet Explorer (as you would expect).

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>

Todecreaseadayinmoment,youcanuseadd

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>
    
24.11.2017 / 12:53