在Python中操作时间之strptime()方法的使用

535次阅读  |  发布于5年以前

strptime()方法分析表示根据格式的时间字符串。返回值是一个struct_time所返回gmtime()或localtime()。

格式参数使用相同的指令使用strftime();它默认为"%a %b %d %H:%M:%S %Y"相匹配的ctime()所返回的格式。

如果字符串不能按格式进行解析,或者如果它具有解析后多余的数据,ValueError被挂起。
语法

以下是strptime()方法的语法:


    time.strptime(string[, format])

参数

下面的指令可以嵌入格式字符串:
指令

返回值

这个返回struct_time所返回gmtime()或localtime()的值。
例子

下面的例子显示 strptime()方法的使用。


    #!/usr/bin/python
    import time

    struct_time = time.strptime("30 Nov 00", "%d %b %y")
    print "returned tuple: %s " % struct_time

    When we run above program, it produces following result:

    returned tuple: (2000, 11, 30, 0, 0, 0, 3, 335, -1)

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8