0
  var date = new Date();

  var first_date = new Date(date); //Make a copy of the date we want the first and last days from
  first_date.setUTCDate(1); //Set the day as the first of the month
 
  var firstDay = first_date.toJSON().substring(0, 10);  
  console.log(firstDay)

  1. I am Working on Javascript Dates, i am stuck with adding 7 days to this date
  2. Thanks in advance
abhi
  • 21
  • 5
  • 3
    Stackoverflow is here to help you with code you've written, not write it for you. Create a Date for the first of the month. *getDay* will tell you which day of the week it is. *setDate* can be used to set the date to some other day. Write some code, then post it. Others may then help you fix it. – RobG Sep 14 '20 at 09:06
  • Why don't you check momentjs, You could do much more with https://momentjs.com/ – Kamlesh Sep 14 '20 at 09:18
  • @Kjjassy - There's absolutely no reason to use a library for this. This is one of the things `Date` does just fine on its own. – T.J. Crowder Sep 14 '20 at 09:24

1 Answers1

-1

var date = new Date();

var first_date = new Date(date); //Make a copy of the date we want the first and last days from
first_date.setUTCDate(1); //Set the day as the first of the month
 
var firstDay = first_date.toJSON().substring(0, 10);

var resultDate = new Date();
resultDate.setDate(first_date.getDate() + 7);
var resultDay = resultDate.toJSON().substring(0, 10); 
console.log("First day: " + firstDay)
console.log("7 days from specific day: " + resultDay)
Kiran Dash
  • 4,816
  • 12
  • 53
  • 84
  • Code-only answers aren't helpful. The OP is left wondering why use UTC methods? Why *toJSON* instead of *toString*? Why *substring*? Etc. – RobG Sep 14 '20 at 09:32