Shehzad009
Shehzad009

Reputation: 1607

Django Want to print the date & time for today

I want to print some sort of time stamp or some sort of function to tell what day and time it is in a template. In my views I have

time = datetime.now()

and in my template I have

{{time}}

All this does is prints out a <type 'datetime.date'> object.

Upvotes: 10

Views: 35091

Answers (2)

JamesO
JamesO

Reputation: 25946

if its just in the template, use now

It is {% now "f" %}

Upvotes: 84

aminho
aminho

Reputation: 565

Normally, this should work:

from datetime import datetime

def a_view(request):
    return render_to_response("a_template.html", {
        'time':datetime.now(),
        }, context_instance=RequestContext(request))

Then render the datetime object in your template:

<p>{{time}}</p>

Use the built-in date filter as described here to format your date if you like.

Upvotes: 5

Related Questions