【Python】日期时间格式化输出

要格式化日期,需要先导入time包:

import time

格式化时间使用time.strftime()方法,该方法的接口文档如下:

def strftime(format, p_tuple=None): # real signature unknown; restored from __doc__
    """
    strftime(format[, tuple]) -> string
    
    Convert a time tuple to a string according to a format specification.
    See the library reference manual for formatting codes. When the time tuple
    is not present, current time as returned by localtime() is used.
    
    Commonly used format codes:
    
    %Y  四位数的年份表示(000-9999)
    %m  月份(01-12)
	%d  月内中的一天(0-31)
	%H  24小时制小时数(0-23)
	%I  12小时制小时数(01-12).
    %M  分钟数 [00,59].
    %S  秒数 [00,61].
    %z  Time zone offset from UTC. 与UTC的时区偏移量
    %a  本地简化星期名称
	%A  本地完整星期名称
	%b  本地简化的月份名称
	%B  本地完整的月份名称
    %c  本地相应的日期表示和时间表示
    %p  本地A.M.或P.M.的等价符.
    
    Other codes may be available on your platform.  See documentation for
    the C library strftime function.
    """
    return ""

另附上一些常用格式的输出:

  1. %Y-%m-%d %H:%M:%S 输出样式:2020-02-03 15:30:32
  2. %Y/%m/%d %H:%M:%S 输出样式:2020/02/03 15:30:32
  3. %Y%m%d%H%M%S 输出样式:20200203153032
发布了77 篇原创文章 · 获赞 156 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_34659777/article/details/104182645