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

  • Home
  • SEARCH
  • 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 810525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:53:56+00:00 2026-05-15T00:53:56+00:00

I’m still a bit slow with Python, so I haven’t got this figured out

  • 0

I’m still a bit slow with Python, so I haven’t got this figured out beyond what’s obviously in the docs, etc.

I’ve worked with Django a bit, where they’ve added some datetime formatting options via template tags, but in regular python code how can I get the 12-hour hour without a leading zero?

Is there a straightforward way to do this? I’m looking at the 2.5 and 2.6 docs for “strftime()” and there doesn’t seem to be a formatting option there for this case.

Should I be using something else?

Feel free to include any other time-formatting tips that aren’t obvious from the docs. =)

  • 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-05-15T00:53:57+00:00Added an answer on May 15, 2026 at 12:53 am

    Nothing built-in to datetime will do it. You’ll need to use something like:

    datetime.time(1).strftime('%I:%M%p').lstrip('0')
    

    Addendum

    As @naktinis points out, this is tailored to the use of this particular strftime parameter. Unfortunately, there is no generic solution if the content of the strftime parameter is unknown or unspecified (e.g. an external parameter), because it becomes a “do what I mean, not what I say” problem.

    Thus, given that you have to know what’s in your strftime parameter, in a more complex case you could solve this as parts:

    tval = datetime.time(1)
    tval_str = (tval.strftime('%A, %B ') + tval.strftime('%d').lstrip('0') 
        + tval.strftime(' %Y, ') + tval.strftime('%I:%M').lstrip('0') 
        + tval.strftime('%p').lower())
    

    or with the re module:

    tval = datetime.time(1)
    tval_str = re.sub(r"^0|(?<=\s)0", "", 
        re.sub(r"(?<=[0-9])[AP]M", lambda m: m.group().lower(), 
        tval.strftime('%A, %B %d %Y, %I:%M%p')))
    

    That said, bear in mind that if the "%p" term gives you uppercase letters, it may be because the user set their locale to work that way, and by changing case you are overriding user preferences, which sometimes leads to bug reports. Also, the user may want something other than “am” or “pm”, such as “a.m.” and “p.m.”. Also note that these are different for different locales (e.g. en_US locale gives AM or PM for %p, but de_DE gives am or pm) and you might not be getting characters in the encoding you assume.

    From the documentation on strftime behavior:

    Because the format depends on the current locale, care should be taken when making assumptions about the output value. Field orderings will vary (for example, “month/day/year” versus “day/month/year”), and the output may contain Unicode characters encoded using the locale’s default encoding (for example, if the current locale is js_JP, the default encoding could be any one of eucJP, SJIS, or utf-8; use locale.getlocale() to determine the current locale’s encoding).

    So, in short, if you think you need to override locale settings, make sure you have a good reason why, so you don’t just end up creating new bugs.

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

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's because you are not creating new pathArr variables and… May 15, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer I find out the problem it was disabled Microsoft services… May 15, 2026 at 4:25 pm
  • Editorial Team
    Editorial Team added an answer It depends on the compiler compatibility you need. If you… May 15, 2026 at 4:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.