简体   繁体   English

如何将动态转换日期传递给 html 元素?

[英]How can i pass dynamic converted date to html element?

I have my count down div like below: Here i have to set data-countdown with dynamic date variable which comes from mmongodb.我的倒计时 div 如下所示: 在这里,我必须使用来自 mmongodb 的动态日期变量设置数据倒计时。 I am using ejs template to store that in below script and convert it into required formate then need to send it below html我正在使用 ejs 模板将其存储在下面的脚本中并将其转换为所需的甲酸盐然后需要将其发送到 html 下面

 <div class="countdown" data-countdown="2021/07/10 18:00:00">
                            <div class="days">
                                <div>&nbsp;</div>
                                <span>days</span>
                            </div>
                            <div class="hours">
                                <div>&nbsp;</div>
                                <span>hours</span>
                            </div>
                            <div class="minutes">
                                <div>&nbsp;</div>
                                <span>minutes</span>
                            </div>
                            <div class="seconds">
                                <div>&nbsp;</div>
                                <span>seconds</span>
                            </div>
                        </div>

I am passing user input from data bast to ejs html element for that i have following script.我将用户输入从数据 bast 传递给 ejs html 元素,因为我有以下脚本。 I am getting the data formate from DB as "Mon Jul 03 2017 00:00:00 GMT+0530" and i am converting using below script to match the formate "2021/07/10 18:00:00".我从 DB 获取数据格式为“Mon Jul 03 2017 00:00:00 GMT+0530”,我正在使用以下脚本进行转换以匹配格式“2021/07/10 18:00:00”。 My problem is how can i send below formatted date to above "data-countdown" attribute in html.我的问题是如何将低于格式化的日期发送到 html 中的“数据倒计时”属性之上。

one more doubt is How can i pass ejs variable into below script.另一个疑问是我如何将 ejs 变量传递到下面的脚本中。 Is it working if i pass same as below.如果我通过与以下相同,它是否有效。

 <script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script>
  <script>
    var ds = "<%=My Date as per DB%>"; // Here my dynamic data comes from mongodb database.
    var date = moment(new Date(ds.substr(0, 16)));
    date.format("YYYY-MM-DD 00:00:00"));
  </script>

you can can achieve that from the server side using ejs syntax In your ejs page您可以在您的 ejs 页面中使用 ejs 语法从服务器端实现这一点

data-countdown= "<%= typeof date != 'undefined'||'' ? date:'' %>"

your code has to be placed in the server您的代码必须放在服务器中

var ds =        // retrieve date from mongodb database.
    var date = moment(new Date(ds.substr(0, 16)));
    const date=date.format("YYYY-MM-DD 00:00:00"));
pass the date to your ejs template

app.get('/',(req,res)=>{
        res.render('page name',date)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM