how to get the hour time?

前端 未结 2 509
深忆病人
深忆病人 2021-01-22 03:22

I\'m working on my python script to get the 12 hours time.

I want to get the 7 hour instead of 07.

self.getControl(4203).setLab         


        
相关标签:
2条回答
  • 2021-01-22 04:01

    You can use lstrip to remove a given character from the left side of the result (if it exists):

    hour = time.strftime("%I").lstrip('0')
    print(hour) 
    
    0 讨论(0)
  • 2021-01-22 04:04

    As Derek mentions in his answer, you can strip leading zeros for a more portable solution.

    >>> print(time.strftime("%I").lstrip('0'))
    2
    

    If you are on a Linux platform that uses glibc though, you have a few additional options.

    Option 1:

    Use %l. Notice that this leaves has a space at the beginning, in place of the 0:

    >>> print(time.strftime("%l"))
     2
    

    Option 2:

    Use %-I. This does not have the leading 0 or leading space.

    >>> print(time.strftime("%-I"))
    2
    

    These two options work on Linux systems that use glibc, because Python uses the strftime() from that library.

    0 讨论(0)
提交回复
热议问题