【问题标题】:in html file, how do I convert DateTime format to Hrs:Min?在 html 文件中,如何将 DateTime 格式转换为 Hrs:Min?
【发布时间】:2020-03-20 23:28:18
【问题描述】:

我正在运行 API REQUEST 来检索课程安排。下面是我在 index.html 文件中的代码

{% for class in listofclasses %}
    <div class="list-classes"><strong>{{ class.ClassDescription.Name }}</strong> with {{ class.Staff.FirstName }} at {{ class.StartDateTime }} </div>
    <br>
{% endfor %}

在上面的代码中,class.StartDateTime 将检索时间如下:2020-03-30T18:00:00

如何将其转换为 6:00pm 之类的内容?还是3/30 下午 6:00

我尝试了以下方法,但它不起作用:

{% for class in listofclasses %}
    <div class="list-classes"><strong>{{ class.ClassDescription.Name }}</strong> with {{ class.Staff.FirstName }} at {{ class.StartDateTime|date:"SHORT_DATE_FORMAT" }} </div>
    <br>
{% endfor %}

请帮忙

【问题讨论】:

    标签: python json api datetime typeconverter


    【解决方案1】:

    怎么样...

    import datetime
    input_time_date = datetime.datetime.strptime('2020-03-20T18:00:00', '%Y-%m-%dT%H:%M:%S')
    new_time_date = input_time_date.strftime('%m/%d at %I:%M%p').replace('at 0', 'at ')
    new_time_only = input_time_date.strftime('%I:%M%p').lstrip('0')
    print(new_time_date)
    print(new_time_only)
    

    03/20 下午 6:00

    下午 6:00

    【讨论】:

    • 您需要先转换日期/时间格式,然后再将其发送到您的模板。
    • 另外,我建议不要使用“类”作为变量名。 “类”是保留字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2016-09-08
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多