猿问

如何创建具有特定日期的动态链接

我需要使用“下周五”的日期作为链接的变量来生成动态链接。


我发现这个代码应该总是在下个星期五日期输出:


    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));

但是我不知道如何使它与与我的HTML页面中的按钮连接的下一个代码一起使用。如何选择下周五日期作为变量?


$(document).ready(function(){


    $('#button').click(function(e) {  

      var date = ;


        window.open( "https://www.mydinamiclink.com/"+date );


    });

});

</script> 


皈依舞
浏览 130回答 1
1回答

一只甜甜圈

您可以与星期五的索引 5 一起使用:nextWeekdayDate$(document).ready(function() {&nbsp; &nbsp; $('#button').click(function(e) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var date = nextWeekdayDate(null, 5);&nbsp; &nbsp; &nbsp; &nbsp; var [yyyy, mm, dd] = date.toISOString().split('T')[0].split('-');&nbsp; &nbsp; &nbsp; &nbsp; if (mm.startsWith('0')) mm = mm.slice(1);&nbsp; &nbsp; &nbsp; &nbsp; window.open(`https://www.mydinamiclink.com/${yyyy}${mm}${dd}`);&nbsp; &nbsp; });});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答