简体   繁体   中英

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. I am using ejs template to store that in below script and convert it into required formate then need to send it below 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. 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". My problem is how can i send below formatted date to above "data-countdown" attribute in html.

one more doubt is How can i pass ejs variable into below script. 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

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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