字符串。時間戳。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))

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章