0

The following code returns me next Friday date. I have added an IF in order to remove the first 0 from the month variable,since I need it without the 0 prefix. But it doesn't work. What is the error I'm doing ?

<script type="text/javascript">

function nextWeekdayDate(date, day_in_week) {
var ret = new Date(date||new Date());
ret.setDate(ret.getDate() + (day_in_week - 1 - ret.getDay() + 7) % 7 + 1);
return ret;
}

var date = new Date();
console.log(nextWeekdayDate(date, 5));

  $(document).ready(function() {
  $('#button').click(function(e) {  
      var date = nextWeekdayDate(null, 5);
      var [yyyy, mm, dd] = date.toISOString().split('T')[0].split('-');

      if ([mm hasPrefix:@"0"] && [string length] > 1) {
  mm = [string substringFromIndex:1];
}

      window.open(`https://www.mydinamiclink.com/`+mm);

                      });

      });


</script>

thanks for your help

Francesco
  • 61
  • 5
  • Note that this gets the UTC date, not the local date, which will be different near midnight for the period of the local daylight saving offset. See [*How to format a JavaScript date*](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?r=SearchResults&s=1|1450.4192) – RobG May 27 '20 at 20:28

1 Answers1

1

Turn the month number into a number and back into a string:

 var [yyyy, mm, dd] = (new Date('2020-05-01')).toISOString().split('T')[0].split('-');
 
 console.log('before leading 0 removal:', mm);
 
 mm = parseInt(mm, 10).toString();
 
 console.log('after leading 0 removal:', mm);
Matt Ellen
  • 9,933
  • 4
  • 61
  • 80