Javascript display date of the day before the current

Asked

Viewed 2,676 times

2

I have following script that works right, but at the turn of the month it does not "mount" correctly the date, for example today, it is showing the date 20181000

var today = new Date();
var dd = today.getDate()-1;

var mm = today.getMonth()+1; 
var yyyy = today.getFullYear();
if(dd<10) 
{
    dd='0'+dd;
} 

if(mm<10) 
{
    mm='0'+mm;
} 
var data_ok = today = yyyy+''+mm+''+dd;


var filename = 'ldrel_'+data_ok+'.txt'

2 answers

4


var hoje = new Date();

var ontem = new Date(hoje.getTime());
ontem.setDate(hoje.getDate() - 1);

var dd = ontem.getDate();
var mm = ontem.getMonth()+1; 
var yyyy = ontem.getFullYear();

if(dd<10) 
{
dd='0'+dd;
}

if(mm<10) 
{
mm='0'+mm;
} 

var data_ok = yyyy+''+mm+''+dd;

var filename = 'LDREL_'+data_ok+'.txt'

console.log(filename);

The task of instruction new Date() is to create a memory location for all the data that a date needs to store. What is missing from this task is the data - what date and time are placed at this point in memory. That’s where the parameters enter.

If you leave the parameters empty, Javascript considers that you want the current date and time for this new date object.

To create a date object for a specific date and time, you have five ways to send values as a parameter to the new Date constructor function():

  1. new date("month dd, yyyy hh:mm:ss")
  2. new date("month dd, Ayyyy")
  3. new date("aa,month,dd,hh,mm,ss")
  4. new date("yy,dd")
  5. new date(milliseconds)

Most methods of a date object serve for reading parts of the date and time information and for changing the date and time stored in the object. These two categories of methods are easily identifiable as they begin with the keyword get or set

objDate.getTime() - milliseconds from 1/1/70 00:00:00 GMT

objDate.setDate(val) - day within the month (1-31)

objDate.getDate() - date within the month

Date

  • Cool, really your script is capturing the correct date, as I do for the final result appear only LDREL_20180930.txt ?

  • @Edvaldolucena The same way you did in your script, see edited response. Don’t forget to mark how you accept if you solved your problem!

2

There’s a much simpler way to do that using setTime(). This command sets the time of the date object of your choice, accepting parameters from -1 to 24. From 0 to 23, he arranges hours correctly, however, with the 24 he passes to the next day, and with the -1 to the last hour of the previous day. That is to say.

var hoje = new Date();
var ontem = new Date().setHours(-1);
ontem = new Date(ontem) // o comando setHours devolve a data em milisegundos

var dataformatada = ontem.toLocaleDateString('pt-BR'); // '30/09/2018'
dataformatada = dataformatada.split('/').reverse().join('') // '20180930'
var filename = 'LDREL_'+dataformatada+'.txt'

console.log(filename); // LDREL_20180930.txt
  • 2

    The return of toLocaleDateString varies according to the browser’s locale, it would be better to specify the locale to ensure that it will always use the Brazilian format: toLocaleDateString('pt-BR'). My browser, for example, is set to en-US (I don’t know why it’s in English) and toLocaleDateString() returns in M/D/YYYY format (9/30/2018)

  • 1

    Thanks for the note, I’ll adjust.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.