如何使用Day.js在浏览器中呈现日期?

时间:2020-10-05 17:50:02

标签: javascript web-frontend dayjs

我想使用Dayjs将日期渲染到<span class="mr-3 text-danger date">元素中。我该怎么做呢?我在HTML文档底部的脚本标签中从CDN导入了dayjs。

<!--This is my html code-->
<div class="d-flex event">
    <span class="mr-3 text-danger date"></span>
    <span class="text-danger">24°C, Fort Portal</span>
</div>

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<html>
    <body>
        <!--This is my html code-->
        <div class="d-flex event">
            <span class="mr-3 text-danger date"></span><br>
            <span class="text-danger">24°C, Fort Portal</span>
        </div>
    </body>
    <script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>
    <script>

        let datesClasses = document.getElementsByClassName("date")

        Array.prototype.forEach.call(datesClasses, element => {
    
            element.innerHTML = dayjs().format('YYYY/MM/DD HH:mm:ss A')

        })
    </script>
</html>