欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python 时间日期转换, time 和 datetime

程序员文章站 2022-05-02 16:32:13
...

Python 时间日期转换, time 和 datetime模块,timestamp、time_tuple、time_string的相互转换


Python time和datetime时间戳和时间字符串相互转换

时间的四种类型

"""时间字符串 / 格式化时间(time_string)"""
time_string1 = "2019/10/12 16:48:24"
time_string2 = "2019-10-12 16:48:24"
time_string3 = "Sat Oct 12 16:48:24 2019"

#-------------------------------------------------------------------------------
        
"""日期时间元组 / 日期时间对象(datetime_tuple / datetime_obj)"""
datetime_obj = datetime.datetime(2019, 10, 12, 16, 48, 24, 198690)    
print(datetime_obj)    # 显示 2019-10-12 16:48:24.198690
 
#-------------------------------------------------------------------------------

"""时间元组 / 时间对象(time_tuple / time_obj)"""
# (1) 适用于 time 模块
time_tuple1 = (2019, 10, 12, 16, 48, 24, 5, 285, 0)

# (2) 适用于 datetime 模块
time_tuple2 = (2019, 10, 12, 16, 48, 24, 198690)  

#-------------------------------------------------------------------------------

"""时间戳(timestamp)"""
timestamp = 1570870104.0 

Python 时间日期转换, time 和 datetime

time 模块

"""时间戳 ----> UTC时间元组"""

# (1) 1570870104.0 ----> (2019, 10, 12, 16, 48, 24, 5, 285, 0)
time_tuple = time.gmtime(1570870104.0)

#-------------------------------------------------------------------------------

"""时间戳 ----> 时间元组"""

# (1) 1570870104.0 ----> (2019, 10, 12, 8, 48, 24, 5, 285, 0)
time_tuple = time.localtime(1570870104.0)

#-------------------------------------------------------------------------------

"""时间元组 ----> 时间戳"""

# (1) (2019, 10, 12, 16, 48, 24, 5, 285, 0) ----> 1570870104.0
timestamp = time.mktime((2019, 10, 12, 16, 48, 24, 5, 285, 0))

#-------------------------------------------------------------------------------

"""时间元组 ----> 格式化时间"""

# (1) (2019, 10, 12, 16, 48, 24, 5, 285, 0) ----> "2019/10/12 16:48:24"
time_tuple = (2019, 10, 12, 16, 48, 24, 5, 285, 0)
formatted_time = time.strftime("%Y/%m/%d %H:%M:%S", time_tuple)

# (2) (2019, 10, 12, 16, 48, 24, 5, 285, 0) ----> "2019/10/12 16:48:24"
time_tuple = (2019, 10, 12, 16, 48, 24, 5, 285, 0)
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)

# (3) (2019, 10, 12, 16, 48, 24, 5, 285, 0) ----> "Sat Oct 12 16:48:24 2019"
formatted_time = time.asctime((2019, 10, 12, 16, 48, 24, 5, 285, 0))

#-------------------------------------------------------------------------------

"""格式化时间 ----> 时间元组"""

# (1) "2019/10/12 16:48:24" ----> (2019, 10, 12, 16, 48, 24, 5, 285, -1)
a1 = "2019/10/12 16:48:24"
time_tuple = time.strptime(a1, "%Y/%m/%d %H:%M:%S")

# (2) "2019-10-12 16:48:24" ----> (2019, 10, 12, 16, 48, 24, 5, 285, -1)
a1 = "2019-10-12 16:48:24"
time_tuple = time.strptime(a1, "%Y-%m-%d %H:%M:%S")

# (3) "Sat Oct 12 16:48:24 2019" ----> (2019, 10, 12, 16, 48, 24, 5, 285, -1)
a2 = "Sat Oct 12 16:48:24 2019" 
time_tuple = time.strptime(a2, "%a %b %d %H:%M:%S %Y")

#-------------------------------------------------------------------------------

""" 时间戳 ----> 格式化时间"""

# (1) 1570870104.0 ----> "2019/10/12 16:48:24"
formatted_time = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(1570870104.0))

# (2) 1570870104.0 ----> "2019-10-12 16:48:24"
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1570870104.0))

# (3) 1570870104.0 ----> "Sat Oct 12 16:48:24 2019"
formatted_time = time.asctime(time.localtime(1570870104.0))

#-------------------------------------------------------------------------------

"""格式化时间 ----> 时间戳"""

# (1) "2019/10/12 16:48:24" ----> 1570870104.0
a1 = "2019/10/12 16:48:24"
timestamp = time.mktime(time.strptime(a1, "%Y/%m/%d %H:%M:%S"))

# (2) "2019-10-12 16:48:24" ----> 1570870104.0
a1 = "2019-10-12 16:48:24"
timestamp = time.mktime(time.strptime(a1, "%Y-%m-%d %H:%M:%S"))

# (3) "Sat Oct 12 16:48:24 2019" ----> 1570870104.0
a2 = "Sat Oct 12 16:48:24 2019" 
timestamp = time.mktime(time.strptime(a2, "%a %b %d %H:%M:%S %Y"))

datetime 模块

"""时间戳 ----> UTC日期时间对象"""

# (1) 1570870104.0 ----> 2019-10-12 8:48:24
timestamp = 1570870104.0
datetime_obj = datetime.datetime.utcfromtimestamp(timestamp)

#-------------------------------------------------------------------------------

"""时间戳 ---->日期时间对象"""

# (1) 1570870104.0 ----> 2019-10-12 16:48:24
timestamp = 1570870104.0
datetime_obj = datetime.datetime.fromtimestamp(timestamp)

#-------------------------------------------------------------------------------

"""日期时间对象 ----> 时间元组"""

# (1) 2019-10-12 16:48:24.198690 ----> (2019, 10, 12, 16, 48, 24, 5, 285, -1)
datetime_obj = datetime.datetime(2019, 10, 12, 16, 48, 24, 198690)
time_tuple = datetime_obj.timetuple()

#-------------------------------------------------------------------------------

"""时间元组 ----> 日期时间对象"""

# (1) (2019, 10, 12, 16, 48, 24, 198690) ----> 2019-10-12 16:48:24.198690
datetime_obj = datetime.datetime(2019, 10, 12, 16, 48, 24, 198690)

#-------------------------------------------------------------------------------

"""格式化时间 ----> 日期时间对象"""

# (1) "2019/10/12 16:48:24.198690" ----> 2019-10-12 16:48:24.198690
formatted_time = "2019/10/12 16:48:24.198690"
datetime_obj = datetime.datetime.strptime(formatted_time, "%Y/%m/%d %H:%M:%S.%f")

# (2) "2019-10-12 16:48:24.198690" ----> 2019-10-12 16:48:24.198690
formatted_time = "2019-10-12 16:48:24.198690"
datetime_obj = datetime.datetime.strptime(formatted_time, "%Y-%m-%d %H:%M:%S.%f")

# (3) "Sat Oct 12 16:48:24.198690 2019" ----> 2019-10-12 16:48:24.198690
formatted_time = "Sat Oct 12 16:48:24.198690 2019"
datetime_obj = datetime.datetime.strptime(formatted_time, "%a %b %d %H:%M:%S.%f %Y")

#-------------------------------------------------------------------------------

"""日期时间对象 ----> 格式化时间"""

# (1) 2019-10-12 16:48:24.198690 ----> "2019/10/12 16:48:24.198690"
string = "2019/10/12 16:48:24.198690"
datetime_obj = datetime.datetime.strptime(string, "%Y/%m/%d %H:%M:%S.%f")
string = datetime_obj.strftime("%Y/%m/%d %H:%M:%S.%f")

# (2) 2019-10-12 16:48:24.198690 ----> "2019-10-12 16:48:24.198690"
string = "2019-10-12 16:48:24.198690"
datetime_obj = datetime.datetime.strptime(string, "%Y-%m-%d %H:%M:%S.%f")
string = datetime_obj.strftime("%Y-%m-%d %H:%M:%S.%f")

# (3) 2019-10-12 16:48:24.198690 ----> "Sat Oct 12 16:48:24.198690 2019"
formatted_time = "Sat Oct 12 16:48:24.198690 2019"
datetime_obj = datetime.datetime.strptime(formatted_time, "%a %b %d %H:%M:%S.%f %Y")
string = datetime_obj.strftime("%a %b %d %H:%M:%S.%f %Y")