Shows wrong date

Asked

Viewed 265 times

2

I have this script:

<SCRIPT LANGUAGE="JAVASCRIPT">
<!--    
var now = new Date();
var mName = now.getMonth() +1 ;
var dName = now.getDay() +2;
var dayNr = now.getDate() +1;
var yearNr=now.getYear();
if(dName==1) {Day = "Domingo";}
if(dName==2) {Day = "Segunda-feira";}
if(dName==3) {Day = "Terça-feira";}
if(dName==4) {Day = "Quarta-feira";}
if(dName==5) {Day = "Quinta-feira";}
if(dName==6) {Day = "Sexta-feira";}
if(dName==7) {Day = "Sábado";}
if(mName==1){Month = "Janeiro";}
if(mName==2){Month = "Fevereiro";}
if(mName==3){Month = "Março";}
if(mName==4){Month = "Abril";}
if(mName==5){Month = "Maio";}
if(mName==6){Month = "Junho";}
if(mName==7){Month = "Julho";}
if(mName==8){Month = "Agosto";}
if(mName==9){Month = "Setembro";}
if(mName==10){Month = "Outubro";}
if(mName==11){Month = "Novembro";}
if(mName==12){Month = "Dezembro";}
if(yearNr < 2000) {Year = 1900 + yearNr;}
else {Year = yearNr;}
var todaysDate =(" " + Day + ", " + dayNr + " de " + Month + " de " + Year);

document.write('  '+todaysDate);

//-->
</SCRIPT>

Upshot: inserir a descrição da imagem aqui

Now I intend to show the day of Friday, June 1:

var now = new Date();
var mName = now.getMonth() +1 ;
var dName = now.getDay() +3;
var dayNr = now.getDate() +2;
var yearNr=now.getYear();
if(dName==1) {Day = "Domingo";}
if(dName==2) {Day = "Segunda-feira";}
if(dName==3) {Day = "Terça-feira";}
if(dName==4) {Day = "Quarta-feira";}
if(dName==5) {Day = "Quinta-feira";}
if(dName==6) {Day = "Sexta-feira";}
if(dName==7) {Day = "Sábado";}
if(mName==1){Month = "Janeiro";}
if(mName==2){Month = "Fevereiro";}
if(mName==3){Month = "Março";}
if(mName==4){Month = "Abril";}
if(mName==5){Month = "Maio";}
if(mName==6){Month = "Junho";}
if(mName==7){Month = "Julho";}
if(mName==8){Month = "Agosto";}
if(mName==9){Month = "Setembro";}
if(mName==10){Month = "Outubro";}
if(mName==11){Month = "Novembro";}
if(mName==12){Month = "Dezembro";}
if(yearNr < 2000) {Year = 1900 + yearNr;}
else {Year = yearNr;}
var todaysDate =(" " + Day + ", " + dayNr + " de " + Month + " de " + Year);

document.write('  '+todaysDate);

//-->
</SCRIPT>

But it’s showing 32 May, as shown in the picture: inserir a descrição da imagem aqui

  • 3

    If dayNr I think it’s obvious that the result will be 32, right? You are working with integers and it does not take into account how many days has the month to go back to 1 of next month.

1 answer

1


With the code var dName = now.getDay() +3 and var dayNr = now.getDate() +2; you nay is adding days to the date, you are first taking the current date number and then adding a value to this number, ie you are not adding two days to the current date, you’re adding number two to number thirty.

To add days you need to manipulate the date and not the value of methods.

Example of how to add days to a date:

var now = new Date();
now.setDate(now.getDate() + 2); // aqui vem a adição de dois dias
var dName = now.getDay(); // aqui não precisa mais somar, já é sexta-feira
var dayNr = now.getDate(); // aqui já é 01/06/2018

...

Additionally, I don’t know if you’re already familiar with vectors/arrays, but you wouldn’t need ifs and elses.

var meses = ["jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "set", "out", "nov", "dez"];
nomeDoMes = meses[now.getMonth()];

With the above example, the variable nomeDoMes will have the value "mai", and you don’t even have to do the "+1", because the vector also starts from scratch.

        var now = new Date();
        now.setDate(now.getDate() + 2); // aqui vem a adição de dois dias
        var dName = now.getDay(); // aqui não precisa mais somar, já é sexta-feira
        var dayNr = now.getDate(); // aqui já é 01/06/2018
        var mName = now.getMonth() +1 ;
        var yearNr=now.getYear();
        
        if(dName==1) {Day = "Domingo";}
        if(dName==2) {Day = "Segunda-feira";}
        if(dName==3) {Day = "Terça-feira";}
        if(dName==4) {Day = "Quarta-feira";}
        if(dName==5) {Day = "Quinta-feira";}
        if(dName==6) {Day = "Sexta-feira";}
        if(dName==7) {Day = "Sábado";}
        if(mName==1){Month = "Janeiro";}
        if(mName==2){Month = "Fevereiro";}
        if(mName==3){Month = "Março";}
        if(mName==4){Month = "Abril";}
        if(mName==5){Month = "Maio";}
        if(mName==6){Month = "Junho";}
        if(mName==7){Month = "Julho";}
        if(mName==8){Month = "Agosto";}
        if(mName==9){Month = "Setembro";}
        if(mName==10){Month = "Outubro";}
        if(mName==11){Month = "Novembro";}
        if(mName==12){Month = "Dezembro";}
        if(yearNr < 2000) {Year = 1900 + yearNr;}
        else {Year = yearNr;}
        var todaysDate =(" " + Day + ", " + dayNr + " de " + Month + " de " + Year);

        document.write('  '+todaysDate);

  • 1

    Reinaldo, take a look at your example, you assign the variable data and then uses now, this example will not work although the intention is correct

  • 1

    I changed the names to the same names as the question, thank you.

  • but it didn’t work, neither date, nor day, nor year nor day

  • 1

    I think you missed editing something so it didn’t work, I put a "snippet" here in the reply, click on run to see the full date working. The code is exactly what you did, with the first lines changed.

Browser other questions tagged

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