Changing text depending on date using javascript

Viewed 145

I'm trying to switch text depending on dates.

 document.getElementById('test').innerHTML = today.toISOString().substring(0, 10) == '2021-12-05' ? 'A' : 'B';
But I don’t want to update the text after this day, for example, if today is '2021-12-05', the text will show 'A' until forever, test B will be displayed before '2021-12-05' . Thanks for your help, much appreciated.

1 Answers

Try this: You need to call new Date() to load the current datetime

document.getElementById('test').innerHTML = new Date() > new Date('2021-12-05') ? 'A' : 'B';
<div id="test"></div>

Related