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 8266051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:01:11+00:00 2026-06-08T05:01:11+00:00

I am trying to use datetime objects, including datetime.month , datetime.day , and datetime.hour

  • 0

I am trying to use datetime objects, including datetime.month, datetime.day, and datetime.hour.

The problem is that these objects (say datetime.month) give values as 1, 2, 3, and so on to 12. Instead, I need these in the format 01,02,03 and so on to 12. There’s a similar issue with days and months.

How can I switch to this format?


I realized this wasn’t a very clear question:

I’m using string formatting to print values from a dictionary I have with timestamps.

So, the expression is roughly:

print "%s-%s-%s"%(date.year, date.month, date.day, etc., len(str) )

My values were originally in the correct "%Y-%m-%d form (such as 2000-01-01). Using the above, I get 2000-1-1.

  • 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-08T05:01:12+00:00Added an answer on June 8, 2026 at 5:01 am

    You can print the individual attributes using string formatting:

    print ('%02d' % (mydate.month))
    

    Or more recent string formatting (introduced in python 2.6):

    print ('{0:02d}'.format(a.month))  # python 2.7+ -- '{:02d}' will work
    

    Note that even:

    print ('{0:%m}'.format(a))  # python 2.7+ -- '{:%m}' will work.
    

    will work.

    or alternatively using the strftime method of datetime objects:

    print (mydate.strftime('%m'))
    

    And just for the sake of completeness:

    print (mydate.strftime('%Y-%m-%d')) 
    

    will nicely replace the code in your edit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that's trying to set some configured DateTime and DateTime::Duration
I'm trying to get my MVC3 application to use MySql database instead of Sql
I am trying to use php datetime object for handling dates. Here is my
I am trying to figure out how to use Validation on Business Objects. Until
I'm trying to use JodaTime in Scala. So I did import org.joda.time.DateTime val dt
I have a list of objects that contain a start/end date. I am trying
I am trying to use an LDAP query to return all computer objects created
I'm downloading a file and trying to use JSON.parse, which should return { dateTime:
I'm trying to use a repeatable section of datetimes in a rails app using
i'm trying use facebook API to upload photo in my fan page. I downloaded

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.