Hello, Is there a way to use this code for more properties on the page. So more different dynamic dates, for different dates on 1 page? I am figuring this out right now, but cant seem to duplicate the code and use different classes, it just doesnt work.
Thanks, but I dont think you understood my question right. With the initial code it is possible to have for example “lose 20 pounds by [X dynamix date +5 days from today]” on the page. Thats what I want, but I want to add another date on my page for another section where I have a second, different dynamic date. Can I realise this with the code youve given me above?
Hi @chakir_zarioh,
The code above will work for the (“lose 20 pounds by [X dynamic date +5 days from today]”), but with minor modifications for the text:
<script>
var d = new Date();
var dplus = new Date();
dplus.setDate(d.getDate()+5);
const options = { weekday: 'short', month: 'short', day: 'numeric' };
let ships = document.querySelectorAll(".today");
ships.forEach(d => {
d.innerHTML = '"Lose 20 pounds by ' + dplus.toLocaleDateString('en-US', options) + generateTh(dplus.getDate()) + '"';
})
function generateTh(d){
let ar = String(d).split('');
if (ar[ar.length-1] == 1 && d != 11) return 'st';
else if(ar[ar.length-1] == 2 && d != 12 && d != 13) return 'nd';
else if(ar[ar.length-1] == 3) return 'rd';
return 'th';
}
</script>
But, you will need a different HTML and JS code for another custom date.
The following code adds two days, please add under the previous script
<script>
var dplus2 = new Date();
dplus.setDate(d.getDate()+2);
const options2 = { weekday: 'short', month: 'short', day: 'numeric' };
let ships2 = document.querySelectorAll(".today2");
ships.forEach(d => {
d.innerHTML = '"Order and get it By ' + dplus.toLocaleDateString('en-US', options2) + generateTh(dplus2.getDate()) + '"';
})
</script>