2
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?

cuh
  • 3,723
  • 4
  • 30
  • 47
  • 1
    *setDate* modifies the date and returns the updated time value. Just do `today.setDate(today.getDate() + 1); console.log(today)` though of course it will now be "tomorrow". ;-) – RobG Aug 04 '20 at 06:21
  • You can do this eaily uinsg moment and format the date in to two digits moment().add(1, 'day').calendar(); – Dhanuka Perera Aug 04 '20 at 06:34

5 Answers5

1

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; 
} 
Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
Mark Taylor
  • 1,128
  • 8
  • 15
1

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: https://www.w3schools.com/js/js_date_formats.asp

User81646
  • 628
  • 4
  • 13
1

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());
Andrew Arthur
  • 1,563
  • 2
  • 11
  • 17
0

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)
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
0

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)
todbott
  • 481
  • 5
  • 9