2

I have this code where I convert the current date to this format 2020-08-20 . But how do I alter it to give me the date 10 days from today and 10 days before today.

eg today is 2020-08-20 I am trying to get 10 days from today 2020-08-30

This is my code

const dateConverter = (dateIn) => {
        var year = dateIn.getFullYear();
        var month = dateIn.getMonth() + 1; // getMonth() is zero-based
        var day = dateIn.getDate();
        return year + "-" + month.toString().padStart(2, "0") + "-" + day.toString().padStart(2, "0");
      }
      
      var today = new Date();
      console.log(dateConverter(today));
Dilhan Bhagat
  • 408
  • 1
  • 10
  • 20

6 Answers6

2

It's a little bit tricky. First set the hours from the date to 12 for avoiding problems with summer/wintertime-changing. Then use getDate add 10 for the extra days and setDate with the new value. Now you have a value in milliseconds, generate out of this a new date to get an dateobject. For the second date subtract 20 days because the original date was changed by the action before and do all other the same.

Format the output for the dates with getFullYear, getMonth and getDate . Because month is handled in JS from 0 to 11 add 1 month. Months and days could be 1-digit but you want it 2 digits, so add before the string "0" and get the last 2 chars of it with slice.
Do the format for both dates and return them as array.

const dateConverter = (dateIn) => {
    dateIn.setHours(12);
    let dateIn10days = new Date(dateIn.setDate(dateIn.getDate() + 10));
    let dateFor10days = new Date(dateIn.setDate(dateIn.getDate() - 20));
    
    let strIn10Days = dateIn10days.getFullYear() + '-' + ('0' +(dateIn10days.getMonth()+1)).slice(-2) + '-' + ('0' + dateIn10days.getDate()).slice(-2);
    let strFor10Days = dateFor10days.getFullYear() + '-' + ('0' +(dateFor10days.getMonth()+1)).slice(-2) + '-' + ('0' + dateFor10days.getDate()).slice(-2);
    return [strFor10Days, strIn10Days];
}
      
let today = new Date();
console.log(dateConverter(today));
Sascha
  • 4,576
  • 3
  • 13
  • 34
1

Try this

const dateConverter = (dateIn) => {
    var year = dateIn.getFullYear();
    var month = dateIn.getMonth() + 1; // getMonth() is zero-based
    var day = dateIn.getDate();
    return year + "-" + month.toString().padStart(2, "0") + "-" + day.toString().padStart(2, "0");
}

var today = new Date();
var numberOfDaysToAdd = 10;
var tenDaysPlus = today.setDate(today.getDate() + numberOfDaysToAdd); 
console.log(dateConverter(today));


var today = new Date();
var numberOfDaysToSubtract = 10;
var tenDaysMinus = today.setDate(today.getDate() - numberOfDaysToSubtract); 
console.log(dateConverter(today));
Chilarai
  • 1,842
  • 2
  • 15
  • 33
1

I would suggest you to use the moment library but you still want plain javascript

const convert = (date) => {
 const pastDate = new Date(date)
 pastDate.setDate(pastDate.getDate() - 10); 
 const futureDate = new Date(date)
 futureDate.setDate(futureDate.getDate() + 10); 
 return { pastDate, futureDate } 
}

call convert function with any date.

Ashok
  • 2,846
  • 1
  • 12
  • 20
1

This code will help you

Reference JavaScript calculating date from today date to 7 days before

for after 10 days just just convert the - to +

    const dateConverter = (dateIn) => { 
         var dates ={};

         var days = 10; // Days you want to subtract



         for(let i=0;i<days;i++){


           var date = dateIn;

           var last = new Date(date.getTime() - (i * 24 * 60 * 60 * 1000));

           var day = last.getDate();

           var month= last.getMonth()+1;

           var year= last.getFullYear();

           dates[i] =  year + "-" + month.toString().padStart(2, "0") + "-" +                             day.toString().padStart(2, "0");


         }


          return dates

      }
      
      var today = new Date();
      console.log(dateConverter(today));
       
Hasan_Naser
  • 204
  • 3
  • 13
  • You only give 9 days after out (the 10th is today). You didn' pay attention to timechange for summer/wintertime: try it with `var today = new Date(2020,9,30,23,15);`, you will get `"2020-10-25"`twice if you live in a timezone with change. – Sascha Aug 19 '20 at 15:17
0

I've been messing around that before as well. But on this Stack Overflow you can find a really good answer: Add days to JavaScript Date

Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}

var date = new Date();

alert(date.addDays(5));

This is the code taken from that post.

For subtracting days, just replace the "+ days" with "- days"

Hope this solved your problem!

0

You can convert all the dates to timestamp and then simply calculate with them:

const dateTimestamp = new Date("2020-10-10").getTime()
const milisecondsInADay = 60*60*24*1000
const milisecondsInTenDays = milisecondsInADay * 10
const beforeDate = new Date(dateTimestamp - milisecondsInTenDays)
const afterDate = new Date(dateTimestamp + milisecondsInTenDays)
console.log("before", beforeDate)
console.log("after", afterDate)
console.log("initially", new Date(dateTimestamp))
messerbill
  • 5,499
  • 1
  • 27
  • 38