[Python] How to convert a datetime to period

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    How can I format a datetime in html from flask render_template? I am getting cabinet table from flask_sqlalchemy where the date is formatted 2025-06-25 10:55:15.500207. I just need the duration of the datetime till now. Example 1day ago or 2hours, 30mins ago or 50 minutes ago.

    this is my try

    HTML:

    <p><span class="writeUp">{{Chat.chat}}</span>
    <small class="text_muted ms-2 mt-2 float-end">{{timeNow - Chat.datecreated}} ago</small></p>


    i got this 1 day, 0:00:00

    MY MODEL:

    class Chats(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    chatcategory = db.Column(db.Integer, db.ForeignKey('courses.id'))
    userId = db.Column(db.Integer, db.ForeignKey('users.id'))
    chat = db.Column(db.String(100000))
    chattagged = db.Column(db.Integer, nullable=True)
    admincomment = db.Column(db.String(1000), nullable=True)
    datecreated= db.Column(db.DateTime, default=datetime.now)

    def __init__(self, chatcategory, chat, userId):
    self.chatcategory = chatcategory
    self.userId = userId
    self.chat = chat

    Continue reading...

Compartilhe esta Página