美文网首页
python编程: datetime, date, str三种类

python编程: datetime, date, str三种类

作者: 寒冰千影 | 来源:发表于2019-03-01 14:42 被阅读0次

    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)

    相关文章

      网友评论

          本文标题:python编程: datetime, date, str三种类

          本文链接:https://www.haomeiwen.com/subject/squduqtx.html