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():
- new date("month dd, yyyy hh:mm:ss")
- new date("month dd, Ayyyy")
- new date("aa,month,dd,hh,mm,ss")
- new date("yy,dd")
- 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 ?
– Edvaldo Lucena
@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!
– user60252