Parse and display dates and time in any timezone. - jordy33/turbogears_tutorial GitHub Wiki

Date and time usually are are managed in UTC time, but How to render this date/time as local-server-timezone?
Well we must use momentz library.

Click here to check library.

Create the file dateformat.mak in templates folder with the following code:

<%inherit file="local:templates.master"/>

<%def name="title()">
Learning TurboGears 2.3: Quick guide to the Quickstart pages.
</%def>
<%def name="head_content()">
 <script src="https://rawgit.com/jordy33/turbogears_tutorial/master/misc/moment.js"></script>
 <script src="https://rawgit.com/jordy33/turbogears_tutorial/master/misc/moment-with-locales.js"></script>
<script>

function dateFmatter ( cellvalue)
{

    var utcDate=moment.utc(cellvalue,"YYYY-MM-DD h:mm:ss")
    var localDate=moment(utcDate).local();
    var formatdate = localDate.format("YYYY-MM-DD HH:mm:ss");
    document.getElementById("text-from-js").innerHTML = formatdate;
    return formatdate;
}
</script>
</%def>

<div class="row">
  <div class="col-md-12">
    <div class="page-header">
      <h2>Map Example</h2>
    </div>
    <body onload="dateFmatter('${mydate}');">
    <p> UTC Date: ${mydate}</p>
    <span>Local Date:</span>
    <span id="text-from-js"></span>
    <br>
    <br>
   </div>
</div>

In root.py add the following code at top:

from datetime import datetime

at bottom:

    @expose('myprojectname.templates.dateformat')
    def dateformat(self):

        mydate =datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
        print(mydate)
        return dict(mydate=mydate)

Test the code:

http://localhost:8080/dateformat
⚠️ **GitHub.com Fallback** ⚠️