Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8581473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:03:33+00:00 2026-06-11T21:03:33+00:00

How to display a datetime object as a string format for printing? Here is

  • 0

How to display a datetime object as a string format for printing?

Here is the string I am trying to print:

'Text %s, on date: %d' % (x.name, x.time)

And here is the error I am getting:

%d format: a number is required, not datetime.datetime
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:03:34+00:00Added an answer on June 11, 2026 at 9:03 pm

    Solution

    You need to change the type: in template or in argument:

    'Text %s, on date: %s' % (x.name, x.time)
    

    But datetime.datetime instance is just an object (it has some kind-of-human-friendly string representation). You can change that to be friendlier this way:

    'Text %s, on date: %s' % (x.name, x.time.isoformat())
    

    You can even format it using strftime():

    'Text %s, on date: %s' % (x.name, x.time.strftime('%A, %B %d, %Y'))
    

    Test

    Lets test it:

    >>> import datetime
    >>> now = datetime.datetime.now()  # our "now" timestamp
    >>> '%d' % now
    
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        '%d' % datetime.datetime.now()
    TypeError: %d format: a number is required, not datetime.datetime
    >>> '%s' % now.isoformat()
    '2012-09-25T22:24:30.399000'
    >>> now.strftime('%A, %B %d, %Y')
    'Tuesday, September 25, 2012'
    

    Alternative – .format()

    String formatting operations (%) are one way of formatting string, but there is also another – preferred – way to format strings: .format() method.

    The reason for that is a different topic, but I can show you how the above examples would look like using .format() method:

    'Text {x.name}, on date: {x.time}'.format(x=x)  # accessing properties
    'Text {0}, on date: {1}'.format(x.name, x.time)  # accessing by positions
    'Text {}, on date: {}'.format(x.name, x.time)  # in Python >2.7 it can be shorter
    'Text {0}, on date: {1}'.format(x.name, x.time.isoformat())  # ISO format
    'Text {0}, on date: {1}'.format(x.name, x.time.strftime('%A, %B %d, %Y'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to format the time from a DateTime object as a string. The
I'm trying to display the month from a datetime object in text so that
I am getting an error when I attempt to display a datetime value in
In SQL Server 2005, why does: PRINT Cast('' AS datetime) display: Jan 1 1900
I want to display a date/time submitted value much the same way as Stack
I want to display the username/last connection date/time and some other info on all
I need to add days to given string date and display calculated date in
I'm trying to display images I've loaded previously on a view but getting no
I want to display the date time (which I've placed on master page); in
I want to convert date time to specify format that is Wed Aug 01

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.