字符串。时间戳。datetime类型 。互转

def datetime_str(today):
    # today = datetime.date.today()
    print(today)
    print(type(today))
    print(today.strftime('%Y-%m-%d'))
    print(type(today.strftime('%Y-%m-%d')))
    '''
    2019-07-25
    <class 'datetime.date'>
    2019-07-25
    <class 'str'>
    '''
    return today.strftime('%Y-%m-%d')
def datetime_timestamp(shijian):
    # timestring = '2016-12-21'
    date = shijian.strftime('%Y-%m-%d %H:%M:%S')
    mtime = int(time.mktime(time.strptime(date, '%Y-%m-%d %H:%M:%S')))  # 1482286976.0
    # print(type(mtime))
    print(mtime)
    return mtime
def get_str_datetime(st):
    print(datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S"))
    print(type(datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S")))
    return datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S")
def get_str_timestamp(shijian):
    # timestring = '2016-12-21'
    mtime = int(time.mktime(time.strptime(shijian, '%Y-%m-%d %H:%M:%S')))  # 1482286976.0
    # print(type(mtime))
    print(mtime)
    return mtime
def get_timestamp_datetime(timestamp):
    # 先转成字符串
    st = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(timestamp))
    # 字符串 转成 datetime类型
    print(datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S"))
    print(type(datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S")))
    return datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S")
def get_timestamp_str(timestamp):
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(timestamp)))
    print(type(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(timestamp))))
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(timestamp))

 

 

 

posted @ 2019-07-25 17:01  殇夜00  阅读(13)  评论(0)    收藏  举报