Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nothing built-in to <code>datetime</code> will do it. You'll need to use something like:</p> <pre><code>datetime.time(1).strftime('%I:%M%p').lstrip('0') </code></pre> <h3>Addendum</h3> <p>As @naktinis points out, this is tailored to the use of this particular <code>strftime</code> parameter. Unfortunately, there is no generic solution if the content of the <code>strftime</code> parameter is unknown or unspecified (e.g. an external parameter), because it becomes a "do what I mean, not what I say" problem.</p> <p>Thus, given that you have to know what's in your <code>strftime</code> parameter, in a more complex case you could solve this as parts:</p> <pre><code>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()) </code></pre> <p>or with the <code>re</code> module:</p> <pre><code>tval = datetime.time(1) tval_str = re.sub(r"^0|(?&lt;=\s)0", "", re.sub(r"(?&lt;=[0-9])[AP]M", lambda m: m.group().lower(), tval.strftime('%A, %B %d %Y, %I:%M%p'))) </code></pre> <p>That said, bear in mind that if the <code>"%p"</code> 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 <em>other</em> than "am" or "pm", such as "a.m." and "p.m.". Also note that these are different for different locales (e.g. <code>en_US</code> locale gives <code>AM</code> or <code>PM</code> for <code>%p</code>, but <code>de_DE</code> gives <code>am</code> or <code>pm</code>) and you might not be getting characters in the encoding you assume.</p> <p>From the <a href="http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior" rel="noreferrer">documentation on strftime behavior</a>:</p> <blockquote> <p>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 <code>js_JP</code>, the default encoding could be any one of <code>eucJP</code>, <code>SJIS</code>, or <code>utf-8</code>; use <code>locale.getlocale()</code> to determine the current locale’s encoding).</p> </blockquote> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload