当前位置:   article > 正文

python时间格式转换_pyspark怎么把str变成struct

pyspark怎么把str变成struct
# str转struct_time
import time
cur_time = "2022-03-12 00:00:00"
time_format = "%Y-%m-%d %H:%M:%S"
cur_time = time.strptime(cur_time, time_format)
print(cur_time)
# time.struct_time(tm_year=2022, tm_mon=3, tm_mday=12, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=71, tm_isdst=-1)
# <class 'time.struct_time'>

# struct_time转str
cur_time = time.strftime(time_format, cur_time)
print(type(cur_time))
# <class 'str'>

# str转datetime
from datetime import datetime
cur_time = datetime.strptime(cur_time, time_format)
print(type(cur_time))
# <class 'datetime.datetime'>

# datetime转str
cur_time = cur_time.strftime(time_format)
print(type(cur_time))
# <class 'str'>

# struct_time转datetime
import datetime
# str转struct_time
cur_time = time.strptime(cur_time, time_format)
cur_time = datetime.datetime(*cur_time[:6])
print(type(cur_time))
# <class 'datetime.datetime'>

# struct_time转datetime
from time import mktime
cur_time = cur_time.strftime(time_format)
print(type(cur_time))
cur_time = time.strptime(cur_time, time_format)
print(type(cur_time))
cur_time = datetime.fromtimestamp(mktime(cur_time))
print(type(cur_time))
# <class 'datetime.datetime'>

"""
struct_time可以比较时间大小
datetime可以加减时间
datetime.timedelta(days=1)
"""
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/66472
推荐阅读
相关标签
  

闽ICP备14008679号