how to get the hour time?

前端 未结 2 511
深忆病人
深忆病人 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: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.

提交回复
热议问题