以下代码将在下星期五返回我。我添加了一个IF以便从month变量中删除第一个0,因为我需要它没有0前缀。但这是行不通的。我在做什么错误?
<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>
感谢您的帮助
答案 0 :(得分:1)
将月份数字转换为数字,然后转换为字符串:
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);