Javascript - Trying to Set Tomorrow's Date Using the Code Below [Duplicate]
var today = new Date();
var tomorrow = today.setDate(today.getDate() + 1)
console.log(tomorrow)

1596607917318

I am getting 13 digit number after using setDate(). How can I get the date in 2 digit format?

2

Date outputs in JS often need some manual processing to be exactly what you want. Try this:

// Create new Date instance
var today = new Date();
var tomorrow = today;

// Add a day
tomorrow.setDate(tomorrow.getDate() + 1)

console.log(formatDateToString(tomorrow));

function formatDateToString(date) { 
  var dd = (date.getDate() < 10 ? '0' : '') 
      + date.getDate(); 

  var MM = ((date.getMonth() + 1) < 10 ? '0' : '') 
      + (date.getMonth() + 1); 

  return dd + "/" + MM; 
} 

The Date object has different methods that you can use to get certain parts of the timestamp.

// for day-month (i.e.: Oct 31 is 31-10
let formatted = `${tomorrow.getDate()}-${tomorrow.getMonth() + 1}`

See more:

setDate has changed the date of today.

Therefore output today and don't assign what's returned by setDate.

    var today = new Date();
    today.setDate(today.getDate() + 1);
    console.log(today.toLocaleDateString());

Month is zero based so getMonth() + 1 returns this month, getDate() + 1 returns tomorrow.

var fecha = new Date();
var year = fecha.getFullYear();
var mes = fecha.getMonth() + 1;
var dia = fecha.getDate() + 1;
var hora = fecha.getHours();
var minutos = fecha.getMinutes();
var segundos = fecha.getSeconds();
var output = `Date: ${dia}/${mes}/${year}`+ '\n' + `Time: ${hora}:${minutos}:${segundos}`;

console.log(output)

Nice question, I recently had to do something similar in VB. Here is a simple javascript version, based on your code:

//this gets the date today
var today = new Date();

//we add one, to get the date tomorrow
var tomorrow = today.getDate() + 1

//if tomorrow is a single digit number, we just pad it with a zero
if (tomorrow < 10) 
{
   tomorrow = '0' + tomorrow 
}

//write to the console
console.log(tomorrow)
Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest