#Getting date for now?

9 messages · Page 1 of 1 (latest)

chilly moss
#

Hi,

How can I create a variable that contains the current year, and then use it within the page?

I've tried this:

<script>
    // Return today's date and time
    const currentTime = new Date()

    // returns the year (four digits)
    const year = currentTime.getFullYear()
</script>

But I can't seem to access the "year" variable from within the HTML?

humble wharf
#

Other options would be to write to a global available var, or do mutate the html with your script

#

but normally vars inside of script tags are not accesible from the outside

chilly moss
#

Thanks, and this will run client side and display the year, so it's always correct?

humble wharf
#

the example will run SSR, if you need something for static sites, you need to user either a global variable or mutate the html within your script

chilly moss
humble wharf
chilly moss
#

Yikes, yeah, tht's not making much sense to me... and the other way is through an env vaible and vite?