• python中date、datetime、string的相互转换


    python中date、datetime、string的相互转换

    import datetime

    import time

    string转datetime

    str = '2012-11-19'

    date_time = datetime.datetime.strptime(str,'%Y-%m-%d')

    date_time

    datetime.datetime(2012,11,19,0,0)

    datetime转string

    date_time.strftime('%Y-%m-%d')

    '2012-11-19'

    datetime转时间戳

    time_time = time.mktime(date_time.timetuple())

    time_time

    1353254400.0

    时间戳转string

    time.strftime('%Y-%m-%d',time.localtime(time_time))

    '2012-11-19'

    date转datetime

    date = datetime.date.today()

    date

    datetime.date(2012,11,19)

    datetime.datetime.strptime(str(date),'%Y-%m-%d') #将date转换为str,在由str转换为datetime

    datetime.datetime(2012,11,19,0,0)

  • 相关阅读:
    每日日报2021.4.14
    每日日报2021.4.13
    每日日报2021.4.12
    每日日报2021.4.9
    每日日报2021.4.8
    每日日报2021.4.7
    每日日报2021.4.6
    每日日报2021 4/22
    每日日报2021 4/21
    每日日报2021 4/20
  • 原文地址:https://www.cnblogs.com/hanbowen/p/10062111.html
Copyright © 2020-2023  润新知